SQL Server and GUID? - sql

I am trying to run this code :
exec myStoredProcedure
Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier),
744,
1,
'test',
'Chrysanthemum.jpg',
'2012-03-26 16:22:17',
1,
28402,
null
But I get the following exception
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'c5b48202-36af-4597-9780-5366d4188f55'.
Why? And how do I fix it?

You can't do a CAST when calling a stored procedure. Either you assign that to a variable before executing the sp, or just call it passing the VARCHAR as is (it probably will do the implicit cast just fine).
DECLARE #GUID UnIQUEIDENTIFIER
SET #GUID = 'c5b48202-36af-4597-9780-5366d4188f55'
EXEC myStoredProcedure #GUID, 744,
1,
'test',
'Chrysanthemum.jpg',
'2012-03-26 16:22:17',
1,
28402,
null

Apparently you cant use cast on the parameter list. I just wanted to be sure so I ran a little test:
create procedure myProc
#param1 uniqueidentifier
as
begin
select 1
end
exec myProc Cast('c5b48202-36af-4597-9780-5366d4188f55' AS uniqueidentifier) -- fails
exec myProc 'c5b48202-36af-4597-9780-5366d4188f55' --ok

You cannot use a calculation in a EXEC command.
You have to do like this:
DECLARE #guid uniqueidentifier
SET #guid = 'c5b48202-36af-4597-9780-5366d4188f55'
exec myStoredProcedure
#guid,
744,
1,
'test',
'Chrysanthemum.jpg',
'2012-03-26 16:22:17',
1,
28402,
null

Related

Use stored procedure in values of an Insert statement

I'm trying to use a stored procedure in an insert statement this is my code:
EXEC sp_executesql #statement=N'insert into Celulares_Empleados(CEL_IMEI,Empl_ID,FH_Asignacion,US_Asigno)
values (#imei, EXEC EMPLEADOS_LEGAJOS #legajo, #date, #usuario)',
#params=N'#imei nvarchar(15), #legajo nvarchar(41), #date datetime, #usuario nvarchar(5)',
#imei=N'353108089985778',
#legajo=N'USUARIO DE PRUEBA - Legajo: 1171',
#date='2020-06-18 22:56:08.367',
#usuario=N'admin'
But I get this message:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'EXEC'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ')'.
I also tried to put the Exec statement between ( ) but I get the same error.
Is there a way to do that?
You cannot call stored procedure as part of VALUES clause in INSERT. What you can do is, first execute the stored procedure and get the return value in a variable. Use the variable as part of your sp_executesql procedure call.
DECLARE #p_Empl_ID int
DECLARE #legajo NVARCHAR(41)= N'USUARIO DE PRUEBA - Legajo: 1171'
EXEC #p_Empl_ID = EXEC EMPLEADOS_LEGAJOS #legajo
EXEC sp_executesql #statement=N'insert into Celulares_Empleados(CEL_IMEI,Empl_ID,FH_Asignacion,US_Asigno)
values (#imei,#Empl_ID , #date, #usuario)',
#params=N'#imei nvarchar(15), #legajo nvarchar(41), #date datetime, #usuario nvarchar(5)',
#imei=N'353108089985778',
#Empl_ID = #p_Empl_ID ,
#date='2020-06-18 22:56:08.367',
#usuario=N'admin'
NOTE: Generally, best practice is to, return the execution status of the procedure as return value. If you want to get some scalar value out of procedure execution, it is preferred to use output parameter and use them in subsequent stages.
DECLARE #p_Empl_ID int
DECLARE #legajo NVARCHAR(41)= N'USUARIO DE PRUEBA - Legajo: 1171'
EXEC EMPLEADOS_LEGAJOS #legajo, #p_Empl_ID OUTPUT

Dynamic SQL Server Nested EXEC

I'm trying to EXEC a stored procedure inside an nvarchar that I a them executing.
I receive the following error, Msg 2812, Level 16, State 62, Procedure Map.AdminServiceLoad, Line 127 [Batch Start Line 2]
Could not find stored procedure ''.
The stored procedures I am referencing exist, I had re-started MSSQL, am I unable to EXEC with an EXEC ?
DECLARE #NewStoredProc nvarchar(max) = '
create procedure [Map].[Load'+#TableName+']
as
begin
DECLARE #MapTable nvarchar(100) = [Map].['+#TableName+']
DECALRE #MapDevlTable nvarchar(100) = [MapDevl].['+#TableName+']
DECLARE #ShapesAreValid bit
DECLARE #PointsAreValid bit
EXEC #ShapesAreValid = Map.AdminServiceValidateShapes #TableName = #MapDevlTable
EXEC #PointsAreValid = Map.AdminServiceValidatePoints #TableName = #MapDevlTable
if(#ShapesAreValid = 1 and #PointsAreValid = 1)
begin
INSERT INTO [Map].['+#TableName+'] SELECT('+#ColsToLoad+') FROM [MapDevl].['+#TableName+']
end
end
'
EXEC #NewStoredProc
return 1
It would need to be EXEC(#cmd) not EXEC #cmd.
Without the parentheses it looks to find a stored procedure with the same name as the string inside #cmd.

Passing a variable into sp_helptext

We want to dynamically pass variables into sp_helptext but because sp_helptext accept a variable passed into the colon; this is causing an error.
My query looks like so:
EXEC sp_helptext N'DatabaseName.dbo.SpName'; --- this works
DECLARE #spName VARCHAR(120) = 'spName'
EXEC sp_helptext N'DatabaseName.dbo.'+#spName+''
And the error:
Msg 102, Level 15, State 1, Line 53
Incorrect syntax near '+'.
The problem is that string operations are not supported for arguments.
This is easy enough to fix. Just do the string operations before the call:
DECLARE #spName VARCHAR(120) = 'spName';
DECLARE #fullName NVARCHAR(MAX) = N'DatabaseName.dbo.' + #spName;
EXEC sp_helptext #fullName;
This is for demonstration. If you are actually passing such values in, then you should be using QUOTENAME().

Operand type clash: NULL is incompatible with SmallIntTable SQL Server

I'm trying to execute a simple stored procedure in SQL Server.
USE [dbname]
GO
DECLARE #return_value int
EXEC #return_value = [dbo].[usp_storprocname]
#ID = NULL,
#SomeID = 123456,
#AddressTypeIDs = NULL
SELECT 'Return Value' = #return_value
GO
What I'm getting is an error:
Msg 206, Level 16, State 2, Procedure usp_storprocname, Line 0 [Batch Start Line 2]
Operand type clash: NULL is incompatible with SmallIntTable
From what I understand, it expects to get some kind of an array with numbers in #AddressTypeIDs parameter. I don't need to filter results by that field. How can I pass a correct value there?
Before you execute the stored proc, Declare a variable of type SmallIntTable.
This will be a table with a single column, as you have learned.
Insert whatever values you wish into that table, and then when you Execute the stored procedure, pass that variable as the value for the #AddressTypeIDs parameter.
e.g. as below
USE [dbname]
GO
DECLARE #return_value INT
DECLARE #AddressTypeIDs dbo.SMALLINTCOL;
INSERT INTO #AddressTypeIDs
VALUES (1);
EXEC #return_value = [dbo].[usp_storprocname]
#ID = NULL,
#SomeID = 123456,
#AddressTypeIDs = #AddressTypeIDs
SELECT 'Return Value' = #return_value

output parameter in stored procedure not working

I am new with stored procedures.
I have simple stored procedure for addition of two numbers as follows:
alter proc simpleProc
(
#Tax int ,
#TotalAmount int,
#sum int output
)
as
BEGIN
set #sum=(#Tax+#TotalAmount)
print #sum
END
As we can see in this #sum is output parameter.
But when I execute it as follows:
exec simpleProc 908,82
It gives me the following error:
Msg 201, Level 16, State 4, Procedure simpleProc, Line 0
Procedure or Function 'simpleProc' expects parameter '#sum', which was not supplied.
I have mentioned #sum as output parameter, but then also its demanding me to input #sum parameter.
What can be the mistake?
Yes it you haven't provided output parameter.
Try this
Declare #op int
exec simpleProc 908,82,#op output
//use op variable
You should give the procedure a variable where the output can be stored
declare #sum int
exec simpleProc 908, 82, #sum output