ERROR: 'Incorrect syntax near ' in declare phrase - sql

I am stuck with an error
SQL Error [102] [S0001]: Incorrect syntax near 'go'
when I declare a variable and execute select * from #variable.
Can anyone help? Thanks in advance
declare #vocabulary_database_schema varchar(50)
set #vocabulary_database_schema = 'dbo'
select * from #vocabulary_database_schema.CONCEPT
go

If you are using SQL Server, there are multiple issues with your code. Most importantly, you need to use dynamic SQL. Something like this:
declare #vocabulary_database_schema varchar(50);
declare #sql nvarchar(max);
set #sql = 'select * from #vocabulary_database_schema.CONCEPT';
set #sql = replace(#sql, #vocabulary_database_schema, 'dbo');
exec sp_executesql #sql;

Related

exec sp_executesql statement causes error "expression of non-boolean type specified in a context where a condition is expected"

Please could someone help me with how to get the following function to work. I have found a northwind example that works fine but by changing it with the syntax that I need. When all the lines are selected I get an error stating an expression of non-boolean type specified in a context where a condition is expected, near 'effec'. The statement I need amended is the following:
select max(person_sid) from dim_person
where [Effective_To] <='2015-10-01'
group by person_id_number
declare #sql nvarchar(50), #dt datetime;
set #dt = '2015-10-01';
set #sql = N'SELECT max(person_sid) FROM dim_person WHERE effective_to <= #date';
exec sp_executesql #sql, N'#date datetime',#dt;
You are defining #sql as nvarchar(50)
However you are filling it with more characters...
Try to enlarge its size,
lets say:
DECLARE #Sql AS NVARCHAR(4000)
use the below script. Give sufficient width for the variable #sql for accommodating the query.
DECLARE #sql nvarchar(MAX), #dt datetime;
set #dt = '2015-10-01';
set #sql = N'SELECT max(person_sid) FROM dim_person WHERE effective_to <= #date';
exec sp_executesql #sql, N'#date datetime',#dt;

Get error in string query

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

Declare variable and use in query

Please consider the following
declare #MyField varchar(255);
set #MyField = 'MyDatabaseField';
select distinct Table.#MyField
from Table
This results in the error Incorrect syntax near #MyField. Then I tried:
select distinct Table.['+#MyField+']
from Table
However, this results in an Incorrect column name error.
How do I correctly use the #MyField in this query? I'm on SQL Server 2008.
Please try executing by building a string.
declare #MyField varchar(255);
set #MyField = 'MyDatabaseField';
exec ('select distinct Table.'+#MyField+' from Table')
Refer sp_executesql (Transact-SQL), Using sp_executesql
You should use dynamic SQL to achieve that. You can use sp_executesql stored proc to do that. Please not that I changed your variable declaration to **N**VARCHAR.
declare #MyField nvarchar(255)
set #MyField = N'MyDatabaseField'
declare #sql nvarchar(max) = N'select distinct ' + #MyField + N' from TableName'
exec sp_executesql #sql

How to pass a table variable using sp_executesql

I'm trying to create a table using sp_executesql but I keep getting an error that says "Incorrect syntax near '#_TableName'. Any idea what I'm doing wrong here?
Here's the code that I'm using:
DECLARE #SQLString NVARCHAR(MAX),
#ParamDefinition NVARCHAR(MAX),
#TableName NVARCHAR(MAX);
SET #TableName = N'[dbo].[MyTable]';
SET #SQLString = N'SELECT * FROM #_TableName;';
SET #ParamDefinition = N'#_TableName NVARCHAR(max)';
EXEC sp_executesql #SQLString, #ParamDefinition,
#_TableName = #TableName;
That yields the error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '#_TableName'.
If I hard code the table name and the column type (I have to do both) then the query works, otherwise I get the incorrect syntax message for both those variables.
In case you're wondering, I want to put this code inside a stored procedure, so that if anyone wants to create or modify a table then they call this stored procedure which can run additional validations.
Figured out the problem.
Apparently sp_executesql expects the parameter definition for a table to be of a table type (see this answer for an example: https://stackoverflow.com/a/4264553/21539).
An easier way to solve this problem was to insert the variables names directly into the SQLStatement string as follows:
DECLARE #SQLString NVARCHAR(MAX),
#TableName NVARCHAR(MAX);
SET #TableName = N'[dbo].[MyTable]';
SET #SQLString = N'SELECT * FROM ' + #TableName + ';';
SET #ParamDefinition = N'#_TableName NVARCHAR(max);
EXEC sp_executesql #SQLString;

Dynamic SQL gives "Incorrect Syntax Near '+'"

I am probably being extremely dumb here but why does this simple dynamic query:
EXEC sp_executesql N'SELECT Id FROM dbo.Widgets WHERE Id = ' + 1;
Give "Incorrect syntax near '+'"
?
You can't formulate the expression as part of the argument. And you shouldn't be concatenating that way anyway (think SQL injection) - you're using sp_executesql already, why not use a proper parameter?
DECLARE #Id INT, #sql NVARCHAR(MAX);
SET #Id = 1; -- presumably this will come from elsewhere
SET #sql = N'SELECT Id FROM dbo.Widgets WHERE Id = #Id;';
EXEC sp_executesql #sql, N'#Id INT', #Id;