Function vs. Stored Procedure in SQL Server

523    Asked by diashrinidhi in SQL Server , Asked on Mar 5, 2021
Answered by dia shrinidhi

To finish an undertaking, information base articles like the put-away systems and Functions contain a bunch of SQL explanations.


Function: Function is compiled and executed every time it is called. This cannot modify the data received as parameters and function must return a value.

Stored Procedure: This is a pre-compiled object as this gets compiled for the first time and its compiled format gets saved as well, which gets executed (compiled code) when it is called.

Basic differences between Function and Stored Procedure are as follows:

Function

Stored Procedure

        The function always returns a value.

Stored Procedure will not return a value, but the procedure can return “0” or n values.

        Functions have only input parameters for it.

Whereas, Procedures can have output or input parameters.

       You can call Functions can be from Procedure.

But the vice-versa is not correct. As you can’t call Procedures from a Function.

n SQL Server advance differences between Stored Procedure and Functions are as follows:

Stored Procedure

Function

You can use Transactions in Procedure.

But, you can’t Transactions in Function.

By using a try-catch block, an exception can be handled in a Procedure.

Whereas, you can’t use try-catch block in a Function to handle the exception.

You can’t utilize Procedures in a SELECT statement.

But Function can be utilized in a SELECT statement.

The procedure allows as DML(INSERT/UPDATE/DELETE) as well as a SELECT statement in it.

Whereas, Function allows only SELECT statement in it.

Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT statement.

The function can be used in the SQL statements anywhere in SELECT/WHERE/HAVING syntax.

Postgresql: password authentication failed for user "postgres"

In the event that I review accurately the user "postgres" has no information base password set on Ubuntu of course. This infers that you could sign in to that account essentially by utilizing the "postgres" OS user account.

Trusting, that you have root access on the case you can do:

sudo -u postgres psql

If this fails with a database "postgres" does not exists error, then you are most likely not running on a Ubuntu or Debian server. In this case, just add template1 to the command:

sudo -u postgres psql template1

If any of those commands fail with an error psql: FATAL: password authentication failed for user "postgres" then examine the file /etc/postgresql/8.4/main/pg_hba.conf: There must be a line like this as the first non-comment line:

local all postgres ident

For the newer versions of the PostgreSQL ident actually might be a peer. That's OK also.

Then Inside the psql shell, you can give the database user "postgres" a password:

ALTER USER postgres PASSWORD 'newPassword';

You can leave the psql shell by pressing Ctrl+D or with the command q.

Now you could give pgAdmin a valid password for the DB superuser.

Interested in SQL ? Check out this SQL Certification course



Your Answer

Interviews

Parent Categories