I have the next script example:
DECLARE #lCode INT
DECLARE #lText VARCHAR(400)
DECLARE #lConditionName VARCHAR(40)
DECLARE #Aux VARCHAR(400)
SET #lCode = 1234;
SET #lConditionName = 'Code'
SET #Aux = '#l' + #lConditionName
SET #lText = 'SELECT * FROM Table WHERE Code = ' + #Aux
PRINT #lText
The result is:
SELECT * FROM Table WHERE Code = #lCode
How can i obtein the value of #Aux Var like this?
SELECT * FROM Table WHERE Code = 1234
DECLARE #lCode INT
DECLARE #lText NVARCHAR(400)
DECLARE #lConditionName NVARCHAR(40)
DECLARE #Aux NVARCHAR(400)
SET #lCode = 1234;
SET #lConditionName = N'Code'
SET #Aux = N'#l' + #lConditionName
SET #lText = N'SELECT * FROM Table WHERE Code = ' + #Aux
PRINT #lText
EXEC sp_executesql #lText, N'#lcode INT', #lcode
You can try this :
DECLARE #lCode INT
DECLARE #lText NVARCHAR(400)
DECLARE #lConditionName NVARCHAR(40)
DECLARE #Aux NVARCHAR(400)
SET #lCode = 1234;
SET #lConditionName = N'Code'
SET #Aux = N'#l' + #lConditionName
SET #lText = N'SELECT * FROM Table WHERE Code = ' + #Aux
set #lText = N'Print ' + #Aux
print #lText
EXEC sp_executesql #lText, N'#lcode INT', #lcode
Output
Print #lCode
1234
Related
How can I store the result of exec into a variable?
Here's my attempt at storing the data in a variable:
DECLARE #oltp VARCHAR(50) SET #oltp = 'emp'
DECLARE #wh VARCHAR(25) SET #wh = '_wh'
DECLARE #DatabaseName SYSNAME = #oltp + #wh;
DECLARE #oltp_data_days NVARCHAR(500)
DECLARE #Params NVARCHAR(500)
DECLARE #vi int
SET #oltp_data_days = N'(SELECT datediff(DAY, MIN(business_date), GETDATE()) AS keep_days
FROM '+QUOTENAME(#DatabaseName)+'..sales
WHERE business_date > ''1/1/2000 00:00:00'')'
EXEC sp_executesql #oltp_data_days = #oltp_data_days
, #Params = N'#vi INT OUTPUT'
, #vi = #vi OUTPUT
I am getting results as Null.
Like this:
declare #sql nvarchar(max) = N'set #vi = (select 3 as keep_days)'
declare #vi int
exec sp_executesql #sql, N'#vi int output', #vi = #vi output
select #vi
Using TSQL
Trying to set the #DynamicValue = 23 which is the parameter it is "equal" to in the below but can't figure it out.
DECLARE #DynamicValue VARCHAR(MAX) = '#PersonID';
DECLARE #PersonID INT = 23;
DECLARE #PlaceID INT = 48;
DECLARE #ThingID INT = 29;
DECLARE #nSQL NVARCHAR(MAX);
SET #nSQL = N'SELECT #Parameter2 = ' + CAST(#DynamicValue AS VARCHAR(MAX));
-- This is where the error is. If I used #PersonID here it would be fine but I want this to be dynamic
EXEC sp_executesql #nSQL,
N'#Parameter2 NVARCHAR(MAX) OUTPUT',
#Parameter2 = #DynamicValue OUTPUT;
SELECT #DynamicValue;
Any ideas? Different approaches?
You just have to pass all the parameters into the dynamic batch that performs the assignment.
Like this:
DECLARE #DynamicValue VARCHAR(MAX) = '#PersonID';
DECLARE #PersonID INT = 23;
DECLARE #PlaceID INT = 48;
DECLARE #ThingID INT = 29;
DECLARE #nSQL NVARCHAR(MAX);
SET #nSQL = N'set #Parameter2 = ' + CAST(#DynamicValue AS VARCHAR(MAX));
EXEC sp_executesql #nSQL,
N'#PersonID int, #PlaceID int, #ThingID int, #Parameter2 NVARCHAR(MAX) OUTPUT',
#PersonID = #PersonID, #PlaceID = #PlaceID, #ThingID = #ThingID, #Parameter2 = #DynamicValue OUTPUT;
SELECT #DynamicValue;
The only way I see:
DECLARE #DynamicValue VARCHAR(MAX) = '#PersonID';
DECLARE #PersonID INT = 23;
DECLARE #PlaceID INT = 48;
DECLARE #ThingID INT = 29;
SELECT #DynamicValue = CAST(CASE #DynamicValue
WHEN '#PersonID' THEN #PersonID
WHEN '#PlaceID' THEN #PlaceID
WHEN '#ThingID' THEN #ThingID END as VARCHAR(MAX))
SELECT #DynamicValue
EzLo's comment above that a variable declared outside of dynamic SQL can't be accessed made me rewrite your code in the following way, seems to work:
SET #nSQL = N'DECLARE #PersonID INT = ' + CAST(#PersonID as varchar)
+' DECLARE #PlaceID INT = ' + CAST(#PlaceID as varchar)
+' DECLARE #ThingID INT =' + CAST(#ThingID as varchar)
+' SELECT #Parameter2 = ' + CAST(#DynamicValue AS VARCHAR(MAX));
There is another way to do the same trick, but you still would have to list all your parameters:
DECLARE #DynamicValue VARCHAR(MAX) = '#PersonID';
DECLARE #PersonID INT = 23;
DECLARE #PlaceID INT = 48;
DECLARE #ThingID INT = 29;
DECLARE #DynamicValues TABLE (Parameter VARCHAR(MAX), Value INT)
INSERT INTO #DynamicValues VALUES
('#PersonID',#PersonID),
('#PlaceID',#PlaceID),
('#ThingID',#ThingID);
SELECT #DynamicValue = CAST(Value as VARCHAR(MAX))
FROM #DynamicValues WHERE Parameter = #DynamicValue
SELECT #DynamicValue
This is so strange! The following code is a t-sql.
BEGIN
DECLARE #cinfo VARBINARY(128) = CAST('aud:83/53784862/1' AS VARBINARY(128));
DECLARE #csinfo VARCHAR(128) = CAST(CONTEXT_INFO() AS VARCHAR(128));
SET #csinfo = '<root><value>' + REPLACE(RIGHT(#cinfo, LEN(#cinfo) - 4), '/', '</value><value>') + '</value></root>';
PRINT #csinfo
SET CONTEXT_INFO #cinfo;
DECLARE #sql VARCHAR(MAX) = '
DECLARE #cinfo VARCHAR(128) = CAST(CONTEXT_INFO() AS VARCHAR(128));
DECLARE #csinfo VARCHAR(512) = ''<root><value>'' + REPLACE(RIGHT(#cinfo, LEN(#cinfo) - 4), ''/'', ''</value><value>'') + ''</value></root>'';
PRINT #csinfo
'
EXEC(#sql)
END
This is the output:
<root><value>83</value><value>53784862</value><value>1</value></root>
<root><value>83</value><value>53784862</value><value>1
Why </value></root> is not appended!
I found the solution.
DECLARE #cinfo VARCHAR(128) = REPLACE(CAST(CAST(CONTEXT_INFO() AS VARCHAR(128)) COLLATE SQL_Latin1_General_CP1_CI_AS AS VARCHAR(128)), CHAR(0), '');
this will repair the string convert from CONTEXT_INFO().
I know this question has been asked, and I already found some solutions in internet.. but I still can not make it work properly.
So.. I have to make a SELECT query and store the result in a variable (I DONT want a table variable).
My problem is that the name of the table is also a variable. The table name changes accordingly to a WHILE, here is my code:
DECLARE #numRecord INT;
DECLARE #maxMacNumber INT;
SET #maxMacNumber = 500;
DECLARE #mac INT;
SET #mac = 0;
DECLARE #res FLOAT;
DECLARE #ap INT;
SET #ap = 0;
DECLARE #apString VARCHAR(2);
DECLARE #numRecordString VARCHAR(20);
DECLARE #tablename VARCHAR(500);
DECLARE #sql NVARCHAR(500);
DECLARE #varDefinition NVARCHAR(200);
WHILE #mac <= #maxMacNumber
BEGIN
SET #numRecord = 6 + #mac * 390;
SET #ap = 0;
WHILE #ap < 2
BEGIN
SELECT #apString = CONVERT(VARCHAR,#ap);
SELECT #numRecordString = CONVERT(VARCHAR, #numRecord);
SELECT #rssiString = CONVERT(VARCHAR, #rssi);
SET #tablename = 'APDB.dbo.AP' + #apString;
SET #sql = 'SELECT RSSI FROM ' + #tablename + ' WHERE ID=' + #numRecordString;
SET #varDefinition = N'#res FLOAT OUTPUT';
EXEC sp_executesql #sql, #varDefinition, #res = #res OUTPUT;
PRINT #res;
-- HERE I WILL DO SOMETHING WITH #res
END;
END;
The problem is that it doesn't print anything when I do PRINT #res...
This is the relevant SQL code:
SET #sql = 'SELECT RSSI FROM ' + #tablename + ' WHERE ID=' + #numRecordString;
SET #varDefinition = N'#res FLOAT OUTPUT';
EXEC sp_executesql #sql, #varDefinition, #res = #res OUTPUT;
PRINT #res;
You are never setting #res in the SQL. Try this:
SET #sql = 'SELECT #res = RSSI FROM ' + #tablename + ' WHERE ID=' + #numRecordString;
SET #varDefinition = N'#res FLOAT OUTPUT';
EXEC sp_executesql #sql, #varDefinition, #res = #res OUTPUT;
PRINT #res;
I am not able to get the INSERT portion of the below procedure to work. Any help would be appreciated....
#currTable varchar(100),
#ID int,
#short_Text varchar(250),
#brief_Descrip varchar(250) = Null,
#needsTranslation varchar(10) = Null,
#prev_LangString varchar(250) = Null,
#lang_String varchar(250) = Null,
#original_lang_String varchar(250) = Null,
#StringID_from_Master int,
#GUID varchar(250) = Null
AS
BEGIN
SET NOCOUNT ON;
DECLARE #submitDate1 DATETIME;
SET #submitDate1 = GETDATE()
SET #prev_LangString = #original_lang_String
DECLARE #sql NVARCHAR(MAX);
SELECT #sql = N' UPDATE ' + QUOTENAME(#currTable) + ' SET [lang_String] = ''' + REPLACE(#lang_String,'''','''''') + ''', [date_Changed] = ''' + convert(varchar(20), #submitDate1) + ''', [prev_LangString] = ''' + #prev_LangString + ''', [needsTranslation] = ''' + #needsTranslation + ''' WHERE ID = ' + RTRIM(#ID) + '; ';
EXEC sp_executesql #sql;
INSERT tblPendingDBUpdates
( stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID
)
SELECT #StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master;
END
I tried this code block only and no luck it did not INSERT to tblPendingDBUpdates.... Noticed something weird about GUID as field name so changed it in table also...
AS
BEGIN
SET NOCOUNT ON;
DECLARE #submitDate1 DATETIME;
SET #submitDate1 = GETDATE()
-- SET #prev_LangString = #original_lang_String
DECLARE #sql NVARCHAR(MAX);
DECLARE #currTable varchar(100);
SET #currTable = 'tblLangenUS'
DECLARE #ID INT;
SET #ID = 2
DECLARE #short_Text varchar(250);
SET #short_Text = 'testing99'
DECLARE #StringID_from_Master INT;
SET #StringID_from_Master = 2
DECLARE #lang_String varchar(250);
SET #lang_String = 'testing9999'
DECLARE #GUID1 varchar(250);
SET #GUID1 = 'Null'
INSERT tblPendingDBUpdates
( stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID1
)
SELECT #StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID1
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master;
END
I don't really know what you need but here is a stored proc with your declarations
CREATE PROCEDURE TestProc
AS
BEGIN
SET NOCOUNT ON;
DECLARE #submitDate1 DATETIME
SET #submitDate1 = GETDATE()
DECLARE #sql NVARCHAR(MAX)
DECLARE #StringID_from_Master INT
SET #StringID_from_Master = 2
DECLARE #short_Text varchar(250)
SET #short_Text = 'testing99'
DECLARE #lang_String varchar(250)
SET #lang_String = 'testing9999'
DECLARE #GUID1 varchar(250)
SET #GUID1 = 'Null'
BEGIN
EXEC sp_executesql #sql
END
BEGIN
INSERT INTO tblPendingDBUpdates
(
stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID1
)
SELECT
#StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID1
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master
END
END
GO
Well as far I understand it ... change the SP code to below and try again ... most probably will work fine
CREATE PROCEDURE <SOME NAME>
AS
BEGIN
<ALL YOUR DECLARATION HERE>
BEGIN
EXEC sp_executesql #sql;
END
GO
BEGIN
INSERT INTO tblPendingDBUpdates
(stringMasterID,
databaseName,
databaseStringID,
englishText,
foreignLangText,
submitDate,
GUID1)
SELECT #StringID_from_Master,
Database_Name,
dbKeyID_ofStringName,
#short_Text,
#lang_String,
#submitDate1,
#GUID1
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master;
END
END
GO
If your not getting an error message and inserting zero rows, than you select statement must not be returning anything.
what does this return?
select *
FROM tblDBUsage
WHERE tblUniquetblStringsMaster_ID = #StringID_from_Master
If you're first stored proc it didn't look like #StringID_from_Master ever got set. That could be your problem.