After decalring vaiables. How do I pass it to a .exe? My code below does not work.
DECLARE #ODBCConn varchar(10)
SET #ODBCConn = 'TestDb'
EXECUTE master..xp_cmdshell '"C:\tmt.exe"' + #ODBCConn
It looks like you are just missing a space after the exe name.
EXECUTE master..xp_cmdshell '"C:\tmt.exe "' + #ODBCConn
You can't perform concatenation when passing a parameter into a stored procedure; the parameter must be a value, and not an expression that would result in a value...
instead, perform the concatenation before calling:
DECLARE #ODBCConn varchar(10)
SET #ODBCConn = 'TestDb'
DECLARE #Cmd varchar(500)
SET #Cmd = '"C:\tmt.exe" ' + #ODBCConn
EXECUTE master..xp_cmdshell #Cmd
Related
I am writing an SP in SQL SERVER, and I need one of the steps to execute a query that comes with data from another variable and save that result in another variable for later use in SP. The code I am using displays the following error:
Procedure expects parameter '#handle' of type 'int'.
DECLARE #retorno INT, #sql NVARCHAR(4000), #parametro NVARCHAR(4000), #caminho NVARCHAR(4000)
SET #caminho = '\\arquivos\CÉLULA DE INOVAÇÃO\SQL\19072019.CSV'
SET #parametro = N'#retornoOut INT OUTPUT'
SET #sql = 'SELECT TOP 1 SUBSTRING(RIGHT('''+#caminho+''',12),1,8)'
PRINT #SQL
EXEC sp_execute #sql, #parametro, #retornoOut=#retorno OUTPUT;
The purpose of the code is from a file passed by the user, I can get the name from the given path. I don't know what could be wrong with id.
The problem is that youre using sp_execute , when you should use sp_executesql
this works fine
DECLARE #retorno INT, #sql NVARCHAR(4000), #parametro NVARCHAR(4000), #caminho NVARCHAR(4000)
SET #caminho = '\\arquivos\CÉLULA DE INOVAÇÃO\SQL\19072019.CSV'
SET #parametro = N'#retornoOut INT OUTPUT'
SET #sql = 'SELECT TOP 1 SUBSTRING(RIGHT('''+#caminho+''',12),1,8)'
PRINT #SQL
EXEC sp_executesql #sql, #parametro, #retornoOut=#retorno OUTPUT;
You don't need to use dynamic SQL for this. You can just use set or a simple select:
Using set:
SET #retorno = SUBSTRING(RIGHT(#caminho,12),1,8)
Using select:
SELECT #retorno = SUBSTRING(RIGHT(#caminho,12),1,8)
I have a script and its works well:
DECLARE #RunSPSQL VARCHAR(60);
SET #RunSPSQL = 'EXEC master.dbo.sp_test';
EXEC (#RunSPSQL) AT LNK_SERVER_NAME;
Now I would like to change linked server name to variable, something like this:
DECLARE #RunSPSQL VARCHAR(60);
DECLARE #LNK_Name NVARCHAR(60);
SET #RunSPSQL = 'EXEC master.dbo.sp_test';
SET #LNK_Name = 'LNK_SERVER_NAME';
EXEC (#RunSPSQL) AT #LNK_Name;
But it doesn't work:
Incorrect syntax near '#LNK_Name'
I was looking for a solution, but no success for now.
If anyone, please help.
You can't use a variable to replace the name of an object. Instead you need to use some dynamic SQL to achieve this:
DECLARE #RunSPSQL varchar(60);
DECLARE #LNK_Name nvarchar(60);
DECLARE #SQL nvarchar(MAX);
SET #RunSPSQL = 'EXEC master.dbo.sp_test';
SET #LNK_Name = N'LNK_SERVER_NAME';
SET #SQL = N'EXEC (#RunSPSQL) AT ' + QUOTENAME(#LNK_Name) + N';';
EXEC sp_executesql #SQL, N'#RunSPSQL varchar(60)', #RunSPSQL = #RunSPSQL;
Just ensure you quote the linked server's name to avoid any injection.
I have a query that I am running in a stored procedure. However, is produces the error:
Incorrect syntax near 'CHEQUE'
The query is:
SELECT #QUERY1 = 'UPDATE [dbo].[ATAB] SET PAYMCODE='CHQ' WHERE RATE=1'
How do I specify this string 'CHQ' without getting an error?
When specifying a string literal within a dynamic SQL string you have to escape the single quotation with another single quotation (ex: '', not " which is double quotation) So the query will be like this:
SELECT #QUERY1 = 'UPDATE [dbo].[ATAB] SET PAYMCODE=''CHQ'' WHERE RATE=1'
This will translate it to:
UPDATE [dbo].[ATAB] SET PAYMCODE='CHQ' WHERE RATE=1
You can also use Nate S's answer if you want to store CHQ into a variable, or use EXEC with specifying parameters like this:
DECLARE #Paymcode varchar(3) = 'CHQ'
DECLARE #SQL nvarchar(max)
DECLARE #Params nvarchar(max)
SET #SQL = N'UPDATE [dbo].[ATAB] SET PAYMCODE=#innerPaymcode WHERE RATE=1'
SET #Params = N'#innerPaymcode varchar(3)'
EXEC sp_executesql #SQL, #innerPaymcode = #Paymcode
You have to escape your single quotes.
SELECT #QUERY1 = 'UPDATE [dbo].[ATAB] SET PAYMCODE=''CHQ'' WHERE RATE=1'
Just to be different...
Declare #PaymMode varchar(3) = 'CHQ';
SELECT #QUERY1 = 'UPDATE [dbo].[ATAB] SET PAYMCODE='+#PaymMode+' WHERE RATE=1';
You need to escape the single quotes in your code
SELECT #QUERY1 = 'UPDATE [dbo].[ATAB] SET PAYMCODE = ''CHQ'' WHERE RATE = 1'
Inside a stored procedure (A) I need to call a stored procedure (X) inside a specific database and capture the output. X returns a single value.
From what I understand I need to provide the DB name of X to the stored procedure in A and I need to use dynamic SQL to build the query on execution targeting the desired database.
What am unable to figure out is how to capture output from X in A to work with the result.
You could use sp_executesql to dynamically call your nested Stored Procedure.
DECLARE #db AS SYSNAME
DECLARE #return_value AS INT
DECLARE #output_value AS INT
DECLARE #sql AS NVARCHAR(MAX)
-- Set your DB name
SET #db = N'mydb'
/*
Use sp_executesql to dynamically pass in the db and stored procedure
to execute while also defining the values and assigning to local variables.
*/
SET #sql = N'EXEC #rtn = ' + #db + '.dbo.[your_stored_procedure] #output OUTPUT'
EXEC sp_executesql #sql
, N'#rtn AS INT, #output AS INT OUTPUT'
, #return_value = #rtn
, #output_value = #output OUTPUT
Adding to the above answer, following is the way to call stored procedures dynamically by passing parameters.
DECLARE #SpName VARCHAR(1000)
SELECT #SpName = DeleteLiveSP FROM dbo.ArchivalInfo (NOLOCK) WHERE TableName = #TableName
DECLARE #SqlString nvarchar(2000)
DECLARE #ParamDef nvarchar(2000)
SET #SqlString = N'exec '+#SpName + ' #CriteriaParam'
SET #ParamDef = N'#CriteriaParam XML'
EXECUTE sp_executesql #SqlString ,#ParamDef, #CriteriaParam = #Criteria
I need to do something like this, but it always fails with 'Error converting data type varchar to int':
DECLARE #intParam INT
DECLARE #ColName VARCHAR(64)
SET #ColName='intcolumn'
SET #intParam = SELECT #ColName FROM myTable
How do I accomplish something like this? I can see the problem is that the SELECT statement simply returns the column name as a string, but I am not sure how to fix that. I am using SQL Server 2008R2.
You need to use dynamic sql:
build your dynamic SQL query (take a look at #SQL variable in sample below)
use output parameter to get value back from dynamic sql (take a look at #intParam and #intParam_out in sample below)
execute dynamic sql using sp_executesql
DECLARE #intParam INT
DECLARE #ColName VARCHAR(64)
SET #ColName='intcolumn'
DECLARE #SQL NVARCHAR(1000)
SET #SQL = 'SELECT #intParam_out = ' + #ColName + ' FROM myTable'
exec sp_executesql #SQL, N'#intParam_out int OUTPUT', #intParam_out = #intParam OUTPUT
Use Cast:
SET #intParam = SELECT cast(#ColName as int) FROM myTable