sp_executesql get xml return - sql

I need to execute a procedure that is encrypted and get the return(That is a XML) and add to a variable. I try with sp_executesql but don't sucess.
here's what a try:
declare #str nvarchar(max)
select #str ='';
dECLARE #Params NVARCHAR(max)
DECLARE #result varchar(max)
SET #Params = N'#resultout varchar(max) OUTPUT'
select #str ='select #resultout = MyProcedure #pageIndex=0,#ascDesc=N''ASC'',#culture=N''pt-br'''
EXEC sp_executesql #str,
#Params,
#resultout=#result OUTPUT
select #result
What is wrong?

Related

I want to use dynamic variable that is declared within dynamic SQL

declare #sql as nvarchar(500)=''
set #sql='
declare #N4 as int = 1
declare #ms as nvarchar(100) = concat(''ms'', convert(nvarchar(10), #N4))
select #ms
'
exec #sql
I want output as ms1.
DECLARE #SQL AS NVARCHAR(500)=''
SET #sql='
while (#i <10)
begin
PRINT (''MS_''+#I)
set #i=#i+1
end
'
EXEC(#SQL)
not generating value for #i
i want to put this code in while loop as I want to access ms1 to ms10
Use sp_executesql which supports ouput params
DECLARE #MS VARCHAR(50)
exec sp_executesql N'declare #N4 as int = 1;
SELECT #MS= concat(''ms'', convert(nvarchar(10), #N4))',
N'#MS VARCHAR(50) output', #MS output;
SELECT #MS
Yes, you can use and for that you need to use sp_executesql like this -
Declare #sql as nvarchar(500)='', #Params NVARCHAR(500),
#N4 Int = 1, #ms nvarchar(100)
SET #Params = '#N4 Int, #ms nvarchar(100) OUTPUT'
set #sql= N'SELECT #ms = concat(''ms'', convert(nvarchar(10), #N4))'
EXEC SP_EXECUTESQL #sql, #Params, #N4 = #N4, #ms = #ms OUTPUT
SELECT #ms
Use While statement and string concatenation to get your result :
DECLARE #StartValue INT = 1
DECLARE #EndValue INT = 10
DECLARE #Query VARCHAR(500) = ''
WHILE #StartValue < #EndValue
BEGIN
SET #Query = #Query + 'sms_' + CAST(#StartValue AS VARCHAR) + ','
SET #StartValue = #StartValue + 1
END
SELECT Query

Set EXECUTE sp_executesql result into a variable in sql

I have to set a dynamic sql query result to a variable. My sql query is :
DECLARE #ResultString NVARCHAR(MAX)
DECLARE #Qry NVARCHAR(MAX)
SET #Qry='SELECT Test FROM MTest22Dec WHERE ID = 1'
EXECUTE sp_executesql #Qry, N'#Result NVARCHAR(MAX) OUTPUT', #Result=#ResultString OUTPUT
PRINT #ResultString
But #ResultString is printing empty string although there is record in database table.
What is wrong in this query?
thanks
You need to assign the result of select to variable inside Dynamic statement.
Change you query like this.
DECLARE #Result NVARCHAR(MAX)
DECLARE #Qry NVARCHAR(MAX)
SET #Qry='SELECT #Result = Test FROM MTest22Dec WHERE ID = 1'
EXECUTE Sp_executesql #Qry, N'#Result NVARCHAR(MAX) OUTPUT', #Result OUTPUT
PRINT #Result
You have to set the output variable somewhere, eg.
SET #Qry='SELECT #Result = Test FROM MTest22Dec WHERE ID = 1'
Try this:
DECLARE #ResultString NVARCHAR(MAX)
DECLARE #Qry NVARCHAR(MAX)
SET #Qry='SELECT #ResultString = Test FROM MTest22Dec WHERE ID = 1'
EXECUTE sp_executesql #Qry, N'#ResultString NVARCHAR(MAX) OUTPUT', #ResultString OUTPUT
PRINT #ResultString
Try this :
Declare #Query NVARCHAR(100)
Declare #ResultString varchar(50)
set #Query =N'select #ResultString=Test from dbo.MTest22Dec where id = #id'
exec sp_executesql #Query, N'#id int,#ResultString varchar(20) output',
#id = 1,#ResultString=#ResultString output
print #ResultString

How to use Pass comma separated string in dynamic query in SQL

I have a function which will return integer values from comma delimited string , It takes two parameters (#string nvarchar(4000), #delimiter char(1)). So the problem is if I am using this function inside a dynamic query I am getting error , here is query
declare #ProductIDs varchar(11)
declare #SQL varchar(max)
set #ProductIDs='1,2,3,4'
declare #query varchar(max)
--set #query= #ProductIDs +','+#Delimiter
SELECT #SQL = 'SELECT val FROM dbo.[fnDelimitedStringToTable]('+ #ProductIDs +' , '','')'
Exec(#SQL)
I am getting error Procedure or function dbo.fnDelimitedStringToTable has too many arguments specified.
When you build a dynamic SQL like that, you need to wrap your parameter in double quote ''
declare #ProductIDs varchar(11)
declare #SQL varchar(max)
set #ProductIDs='1,2,3,4'
declare #query varchar(max)
--set #query= #ProductIDs +','+#Delimiter
SELECT #SQL = 'SELECT val FROM dbo.[fnDelimitedStringToTable]('''+ #ProductIDs +''' , '','')'
Exec(#SQL)
This way the SQL statement will be:
SELECT val FROM dbo.[fnDelimitedStringToTable]('1,2,3,4' , '','')
and not:
SELECT val FROM dbo.[fnDelimitedStringToTable](1,2,3,4 , '','')
Use sp_executesql instead. In this case you can pass arguments as parameters.
DECLARE #SQL nvarchar(max)
DECLARE #ParmDef nvarchar(1000)
DECLARE #ArgProductIDs nvarchar(100)
DECLARE #Arg2 nvarchar(100)
DECLARE #Arg3 nvarchar(100)
SET #SQL = N'SELECT val
FROM dbo.[fnDelimitedStringToTable](#ProductIDs, #Param2, #Param3)';
SET #ParmDef = N'#ProductIDs nvarchar(100),
#Param2 nvarchar(100),
#Param3 nvarchar(100)';
SET #Arg1 = N'1,2,3,4';
SET #Arg2 = N'';
SET #Arg3 = N'';
EXEC sp_executesql #SQL, #ParmDef,
#ProductIDs = #ArgProductIDs, #Param2 = #Arg2, , #Param3 = #Arg3

Conversion failed while converting nvarchar value to datatype int

I want to get the values of #sql to p.
But while conversion it says that "Conversion failed when converting the nvarchar value 'select sum(stock) from[Hard disk]' to data type int."
declare #tname varchar(10);
declare #p int
declare #i int
declare #sql nvarchar(max);
set #tname = 'Hard disk';
set #sql = 'select sum(stock) from' + '[' + #tname + ']'
exec (#sql)
print #sql
select #p = convert(int,#sql)
print #p
What i want is to assign the result of the query #SQL in integer variable #p..
One way to do it is do it all in the dynamic sql.
declare #tname varchar(10);
declare #p int
declare #i int
declare #sql nvarchar(max);
set #tname = 'Hard disk';
set #sql =
'DECLARE #Result AS INT
select #Result = sum(stock) from' + '[' + #tname + ']
PRINT #Result'
exec (#sql)
The other way would be creating an output param.
DECLARE #tname VARCHAR(50)
DECLARE #SQLString NVARCHAR(500)
DECLARE #ParmDefinition NVARCHAR(500)
DECLARE #Result INT
SET #tname = 'Hard disk';
SET #SQLString = N'SELECT #Result = sum(stock)
FROM ' + QUOTENAME( #tname )
SET #ParmDefinition = N'#Result INT OUTPUT'
EXECUTE sp_executesql
#SQLString,
#ParmDefinition,
#Result=#Result OUTPUT
PRINT #Result
You better use QUOTENAME for embrasing the table name with the brackets, as it is more native.
declare #p int;
EXEC SP_EXECUTESQL N'select #p = sum(stock) from '[' + #tname + ']',N'#p int OUTPUT',#p OUTPUT;
select #p

Get SQL result into variable

Here is what I have so far. I want to store the query result into an int variable but I get null so far. After execute, both #query and #countrow are both null. Any suggestion would be great.
SET #query = N'select #countrow=count(*) from ' + #tablename
EXECUTE sp_executesql #query
DECLARE #i INT, #sql NVARCHAR(512), #tablename varchar(200) = 'tbl'
SET #sql = N'SELECT #i = COUNT(*) FROM ' + #tablename
EXEC sp_executesql
#query = #sql,
#params = N'#i INT OUTPUT',
#i = #i OUTPUT
PRINT #i
Take a look at SQL Fiddle
You need to use the OUTPUT keyword, similar to this:
declare #query nvarchar(max)
declare #countrow int
declare #tablename varchar(50)
SET #query = N'select #cnt=count(*) from ' + #tablename
EXECUTE sp_executesql #query, N'#cnt int OUTPUT', #cnt=#countrow OUTPUT
select #countrow as countrow -- to get the result