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'
Related
Hello: I am trying to encapsulate the following Insert and select statement into a variable for SQL to run.
The issue occurs when the equals sign is included for evaluation (or right around there)
The CustomPollerAssignmentID Column had to be cast since it natively a PK and a uniqueidentifier (see image)
What must be done to have the statement evaluate properly when passes to the sp_executesql?
I receive a syntax error as illustrated below
declare
#date datetime,
#query nvarchar(Max)
set #date = getdate()-4
set #query = --'Insert into [SolarWindsOrion].[dbo].[BWC_VPN_Hourly_Long_Store]
'SELECT * FROM [SolarWindsOrion].[dbo].[CustomPollerStatistics_Hourly]
where Cast(CustomPollerAssignmentID as nvarchar(max)) = 63FEB60-4516-4C1A-9A11-DB30ACA44301'
EXEC sp_executesql #query
UPDATE-->
I tried adding the single quotes.
While the statement does execute.. no results are returned. See Image below
you need to put it in quote like this '63FEB60-4516-4C1A-9A11-DB30ACA44301' , so your adhoc query would look like this :
declare
#date datetime,
#query nvarchar(Max)
set #date = getdate()-4
set #query = --'Insert into [SolarWindsOrion].[dbo].[BWC_VPN_Hourly_Long_Store]
'SELECT * FROM [SolarWindsOrion].[dbo].[CustomPollerStatistics_Hourly]
where Cast(CustomPollerAssignmentID as nvarchar(max)) = ''63FEB60-4516-4C1A-9A11-DB30ACA44301'''
EXEC sp_executesql #query
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 am using string query in sql and executing it with Execute method. I have to add an additional column with default value. that default value is contained in a variable. When i add that default valued column in the query string and execute it I get an error "Invalid column name 'dafaultValue' ".
Is there any other solution to do this ?
Here is my code:
Declare #variable Varchar(Max);
Declare #variableQuery Varchar(Max);
set #variable = 'Test';
set #variableQuery = 'select '+#variable+' as dest ,* from myTable ';
execute (#variableQuery);
Your issue is that you need to contain the variable in single quotes:
set #variableQuery = 'select '+ ''''+#variable+'''' +' as dest ,* from myTable';
Otherwise it thinks it is a column name in the table, which it isn't, hence the error.
I would strongly encourage you to learn to use sp_executesql and pass in parameters. This is much safer than munging query strings:
Declare #variable Varchar(Max);
Declare #variableQuery Varchar(Max);
set #variable = 'Test';
set #variableQuery = 'select #variable as dest, t.* from myTable t';
exec sp_executesql #variableQuery,
N'#variable varchar(max)',
#variable = #variable;
This method also allows you to pass values back from the query, so for processing queries, it is more powerful than exec.
The query variable would often be declared to be nvarchar(), just to be as general as possible.
I'm a beginner to SQL Server
I wrote this query:
DECLARE #sql nvarchar(1000) = 'UPDATE Work
SET [Name] = Programmer, [ImageAddress] = pic.jpg
WHERE Id = 2'
SELECT #sql
EXEC Sp_executesql #sql
but I get this error
Invalid column name 'Programmer'.
Why do I get this error?
Thank you for your help
You are dealing with SQL in strings. Quoting the strings becomes a challenge. You need for Programmer to be in single quotes when the query is executed. To get this, you need double single quotes in the string:
DECLARE #sql nvarchar(1000)='
UPDATE Work
SET [Name] = ''Programmer'', [ImageAddress] = ''pic.jpg'' WHERE Id=2'
select #sql
EXEC Sp_executesql #sql;
Because you are wise enough to use sp_executesql, you should learn about parameters. You can write the query as:
DECLARE #sql nvarchar(1000)='
UPDATE Work
SET [Name] = #Programmer, [ImageAddress] = #imageaddress WHERE Id=2'
select #sql
EXEC Sp_executesql #sql, N'#programmer nvarchar(255), #imageaddress nvarchar(255)',
#programmer = N'Programmer', #imageaddress = N'pic.jpg';
This has several advantages besides the quoting. It is safer in terms of SQL injection and it allows SQL Server to cache the execution plans if the query is called more than once.
try this:
You need to use '' (Double Quotes for string) Inside Dynamic SQL
DECLARE #sql nvarchar(1000)='
UPDATE Work
SET [Name] = ''Programmer'',[ImageAddress] =''pic.jpg'' WHERE Id=2'
select #sql
EXEC Sp_executesql #sql
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