Combining Stored Procedure Statement causes error in SQL Server - sql

When I use the query individually then it works. But it fails when I combine both the Stored Procedure statements.
spMarkAsApproved is a Store Procedure with Update statement.
This works
spMarkAsApproved '112', 'A';
This also works
spMarkAsApproved '113', 'A';
This Doesn't work
spMarkAsApproved '112', 'A';
spMarkAsApproved '113', 'A';
Error Mesage
Incorrect syntax near 'spMarkAsApproved'.
Any suggestion will be helpful.

A stored procedure can be called and executed without the EXEC keyword if the procedure is the first statement in the Transact-SQL batch.
https://learn.microsoft.com/en-us/sql/relational-databases/stored-procedures/execute-a-stored-procedure?view=sql-server-ver16
exec spMarkAsApproved '112', 'A';
exec spMarkAsApproved '113', 'A';

As per the documentation if you are calling a procedure and it is the first statement in the batch, then the EXEC keyword can be omitted:
Remarks
...
You do not have to specify the EXECUTE keyword when executing modules if the statement is the first one in a batch.
Examples: SQL Server
...
If the following is the first statement in a batch or an osql or sqlcmd script, EXEC is not required.
dbo.uspGetEmployeeManagers 6;
GO
--Or
dbo.uspGetEmployeeManagers #EmployeeID = 6;
GO
If the statement is therefore not the first statement, then EXEC must be used.
Your second statement is not the first statement, so EXEC must be used:
spMarkAsApproved '112', 'A';
EXEC spMarkAsApproved '113', 'A';
Better yet, if I am honest, always use EXEC(UTE):
EXEC spMarkAsApproved '112', 'A';
EXEC spMarkAsApproved '113', 'A';

Related

Delete Data from all tables in Sqlserver Database except some tables

I have A SQLSERVER Database, I want to Delete all tables except some tables
i use use this script
EXEC sp_MSforeachtable 'IF OBJECT_ID(''?'') NOT IN (
ISNULL(OBJECT_ID(''[dbo].[T1]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T3]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T4]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T5]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T6]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T7]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T8]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T9]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T10]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T11]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T12]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T13]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T14]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T15]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T16]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T17]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T18]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T19]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T20]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T21]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T22]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T23]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T24]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T25]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T26]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T27]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T28]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T29]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T30]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T31]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T32]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T33]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T34]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T35]''),0)
)
DELETE FROM ?'
the sqlserver return this error message "Msg 102, Level 15, State 1, Line 22
Incorrect syntax near 'ISN'."
I think the problem may be about the number of tables which is excepted
Try to do it another way:
DECLARE #command nvarchar(max);
--Remove spaces in front of ,ISNULL
SELECT #command = N'IF OBJECT_ID(''?'') NOT IN (
ISNULL(OBJECT_ID(''[dbo].[T1]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T2]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T3]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T4]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T5]''),0)
...
,ISNULL(OBJECT_ID(''[dbo].[TN]''),0)
)
DELETE FROM ?';
EXEC sp_MSforeachtable #command;
NOTE: This SP works only with nvarchar(2000) in first command (source).
It is the first command to be executed by this Stored Procedure and the data type is nvarchar(2000).
EXEC sp_MSforeachtable N'IF OBJECT_ID(''?'') NOT IN (ISNULL(OBJECT_ID(''[dbo].[T1]''),0),ISNULL(OBJECT_ID(''[dbo].[T2]''),0),ISNULL(OBJECT_ID(''[dbo].[T3]''),0),ISNULL(OBJECT_ID(''[dbo].[T4]''),0),ISNULL(OBJECT_ID(''[dbo].[T5]''),0),ISNULL(OBJECT_ID(''[dbo].[T6]''),0),ISNULL(OBJECT_ID(''[dbo].[T7]''),0),ISNULL(OBJECT_ID(''[dbo].[T8]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T9]''),0),ISNULL(OBJECT_ID(''[dbo].[T10]''),0),ISNULL(OBJECT_ID(''[dbo].[T11]''),0),ISNULL(OBJECT_ID(''[dbo].[T12]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T13]''),0) ,ISNULL(OBJECT_ID(''[dbo].[T14]''),0),ISNULL(OBJECT_ID(''[dbo].[T15]''),0),ISNULL(OBJECT_ID(''[dbo].[T16]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T17]''),0),ISNULL(OBJECT_ID(''[dbo].[T18]''),0),ISNULL(OBJECT_ID(''[dbo].[T19]''),0),ISNULL(OBJECT_ID(''[dbo].[T20]''),0),ISNULL(OBJECT_ID(''[dbo].[T21]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T22]''),0),ISNULL(OBJECT_ID(''[dbo].[T23]''),0),ISNULL(OBJECT_ID(''[dbo].[T24]''),0),ISNULL(OBJECT_ID(''[dbo].[T25]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T26]''),0),ISNULL(OBJECT_ID(''[dbo].[T27]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T28]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T29]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T30]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T31]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T32]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T33]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T34]''),0)
,ISNULL(OBJECT_ID(''[dbo].[T35]''),0)
)
DELETE FROM ?'
The issue is that sp_msforeachtable has first parameter of length 2000 to which your query is passed. Since your query is of length more than that, full text of it is not passed to proc. That's why the error. Shorten the length of query by removing some line breaks. Try my query its working

Error with OPENQUERY

I have this proble.
I should to call a Function at a LinkedServer database. So this database not show me a Function.
For this I use this code:
BEGIN
DECLARE #TSQL varchar(8000)
DECLARE #Data as DATE
SET #Data = GETDATE()
SET #TSQL = 'SELECT PIPPO FROM OPENQUERY([SQLIMELTC\IMELTCPROD],''SELECT [FlexNet].[dbo].[AF_GetUTCToLocal] (''''' +CAST(#Data AS NVARCHAR(100))+ ''''') AS PIPPO'' )'
EXEC #TSQL
END
I have this error:
Msg 203, Level 16, State 2, Line 10
The name 'SELECT PIPPO FROM OPENQUERY([SQLIMELTC\IMELTCPROD],'SELECT [FlexNet].[dbo].[AF_GetUTCToLocal] (''2015-01-08'') AS PIPPO' )' is not a valid identifier.
But, if I try to use this code before the instruction
EXEC #TSQL
and use it:
SELECT #TSQL
And after the execute I select in output box the result and execute it, it works.
What is my problem?
Look at the EXECUTE (Transact-SQL) syntax. When you use the variable you should use it in brackets.
Try this:
EXEC (#TSQL)
You need to put parenthesis around #TSQL, e.g.
EXEC (#TSQL);
EXEC without parenthesis is for executing stored procedures. e.g.
DECLARE #TSQL VARCHAR(8000) = 'sp_lock';
EXEC #TSQL;
Will execute the stored procedure sp_lock
Better still use sp_executesql, thus avoiding ambiguity:
EXECUTE sp_executesql #TSQL;

Single line GO(Batch) statement giving error in SQL Server?

I have one question. I was creating below procedure temporary.
When I Execute it in below format it works fine:
CREATE PROCEDURE Get_TableList_By_Name
#Proc_Name VARCHAR(255)
AS
BEGIN
SELECT * FROM sys.tables WHERE name LIKE '%' + #Proc_Name + '%'
END
GO
EXEC Get_TableList_By_Name 'norway'
GO
DROP PROCEDURE Get_TableList_By_Name
GO
But when I execute same SQL in below format it giving error saying: "Incorrect syntax near 'GO'."
CREATE PROCEDURE Get_TableList_By_Name #Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + #Proc_Name + '%' END GO EXEC Get_TableList_By_Name 'norway' GO DROP PROCEDURE Get_TableList_By_Name GO
CREATE PROCEDURE Get_TableList_By_Name #Proc_Name VARCHAR(255) AS BEGIN SELECT * FROM sys.tables WHERE name LIKE '%' + #Proc_Name + '%' END GO 1 EXEC Get_TableList_By_Name 'norway' GO 1 DROP PROCEDURE Get_TableList_By_Name GO 1
How to write same SQL with GO statement in single line?
Is it possible? If not then Why?
Thanks,
Vishal
From GO (Transact-SQL)
A Transact-SQL statement cannot occupy the same line as a GO command.
However, the line can contain comments.
So Go needs to be on its own line, except for comments.
'GO' is not a SQL command. It is a batch terminator recognized by tools
like Query Analyzer, SSMS, SQLCMD, etc. These tools generally require the
GO to be on a separate line and send the preceding SQL statements as a batch
to SQL Server when the GO is encountered
GO Statement must be written in new line as it is not T-SQL command.
T-SQL statement can not occupy the same line as GO. GO statement can contain comments.
You can execute this:
EXEC SP_COMMAND 1,2;
EXEC SP_COMMAND 2,2;
EXEC SP_COMMAND 3,2;
Set EXEC Bebore sp and ';' for end statement
then
select all code (SSMS) and press f5 o exec all you code by other thecnic.

EXEC with an EXEC

Is it possible to have an EXEC within an EXEC in SQL?
e.g. EXEC('EXEC ...')
In SQL Server, yes. try:
EXEC ('EXEC sp_helpdb')
or
EXEC ('EXEC (''EXEC sp_helpdb'')')
Yes. With an exec statement you can call any statement you could write into a Management Studio query batch including another exec statement.

Counting results of stored procedure

I have a stored procedure returning ID, Name, Descriptions and takes no input parameters. However, I am interested in how many results do I get.
I expected something like this work:
SELECT COUNT(*) FROM EXEC MyStoredProcedure
But I get the following error in SqlServer Managment Studio:
Incorrect syntax near the keyword 'EXEC'.
Could you show me a little code example how can I do that?
This won't work. May I suggest:
exec MyStoredProcedure
select ##rowcount
Alternatively you could return the count as an output parameter
SELECT ##ROWCOUNT
You need to put the logic in the stored proc and return the count from the stored proc. You do this by using the ##ROWCOUNT variable immediately after your query. This ihow it would work in MS SQL Servet at least.
Stored Proc:
CREATE PROC MyPROC
AS
DECLARE #MyCount int
...
SELECT * FROM MyTable WHERE ...
SELECT #MyCount = ##ROWCOUNT
...
return #MyCOunt
Calling code:
DECLARE #MyCount int
EXEC #MyCount = EXEC MyProc
Write a new stored procedure which does a count for you.