I need to run a SELECT for two different situations. Both SELECTs are almost identical except a few lines which I would like to add with if condition. However, I constantly get an error. Is it possible to use either IF or CASE syntax in order to achieve the desired result?
I have:
DECLARE #isQuarter varchar(5) = true
SELECT [result] = res.Result
FROM Custom_results cr
WHERE cr.index = 12
IF #isQuarter = true
BEGIN
OPTION(RECOMPILE);
END
I get this error:
[SQL Server]Incorrect syntax near the keyword 'OPTION'.
If I use CASE WHEN THEN instead of IF, I get another error:
[SQL Server]Incorrect syntax near the keyword 'CASE'.
Related
My statement:
insert into target_scd2
select id,name,flag from (
merge into target_scd2 as t
using source as s
on t.id=s.id
when matched and t.name <> s.name then
update set flag='N'
when not matched then
insert values(s.id,s.name,'Y')
OUTPUT $Action action_flag,s.id,s.name,'Y'
) as merge_out
where merge_out.action_flag='UPDATE';
I am getting below error while executed the above statement:
SQL compilation error: syntax error line 3 at position 0 unexpected 'merge'. syntax error line 9 at position 29 unexpected ')'.
Can you please help what can be the issue
The pattern presented here is called: "INSERT over DML" and it is SQL Server specific.
More info: D. Inserting the results of the MERGE statement into another table
Can you please help what can be the issue
a) Using merge as subquery of INSERT SELECT
b) OUTPUT/(RETURNING) clause support
The Snowflake documentation of MERGE.
I'm trying to use the stored procedure sp_rename to change column names. However, I get errors when trying to reference the columns because they contain the symbols [ and ]. I can't even to a select:
select [Materials].[All] from Temp_Table
Msg 4104, Level 16, State 1, Line 1
The multi-part identifier "Materials.All" could not be bound.
"[Materials].[All]" is the name of the column. I've tried it like this:
select "[Materials].[All]" from Temp_Table
select [[Materials].[All]] from Temp_Table
Do you see any solutions? Thanks.
EDIT: Ok, some solutions have been presented to make it work in a select statement, but I still get an error when using it in the stored procedure:
EXEC sp_rename '[DBName].[Temp_Table].[Materials].[Material Type].[All]]', 'All', 'COLUMN';
Error: Syntax error parsing SQL identifier '[KS_Control_Area].[Temp_Table_MaterialType_Mios].[Materials].[Material Type].[All]]'.
You need to quote you column's name correctly. So, for a column named silly[column]name you would use [silly[column]]name]. Note that the [ inside the name doesn't need be escaped, however the ] does (to ]]).
I want to run the following query:
Qry.SQL.Text := 'declare #test bit; set #test = 1; select #test;';
Qry.Open;
This causes the error "Incorrect syntax near the keyword 'declare'", but the Query shows the expected "1".
If I run that sql in the Microsoft Management-Studio everything works fine.
So why I get the error and how to solve it? Anyone an idea?
Regards
Maggi
I need to apply an EXCEPT (same as MINUS in Oracle) operation in TSQL. I tried with a simple code
select * from Table1
except
select * from Table1 where calndr_dt between '2014-10-01' and '2014-10-10'`
Each sub query is executing fine. But when joined with EXCEPT, it is returning the following error message.
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 2, column: 1: Incorrect syntax near 'except'.
Remove the ORDER BY clause from the first query, if you have this clause.
I am trying to execute a very simple SQL query using "Microsoft Query". I can connect to the tables, select the columns I need but when I try to execute I get error msg saying "Incorrect Syntax near keyword 'IS'"
I got the SQL statement below through automated query but it just doesn't execute successfully. I think I know what the issue is. It is because my database catalog name is "IS". I tried executing same query on my other databases with different names and it works fine. Since I have access to several databases I need to specifiy which db I am accessing in my script and that's when it causes this issue. Is there a work around in my situation where I can avoid using database name and perhaps declare a variable?
SELECT Table1.id,
Table1.Name,
Table1.Status,
Table1.DateEntered
FROM IS.dbo.Table1 Table1
OR
SELECT * FROM IS.dbo.Table1 Table1 (Same error msg)
IS is a SQL reserved keyword, you have to wrap it with []
SELECT * FROM [IS].dbo.Table1 Table1 (Same error msg)
however, is a good practice - and error avoiding technique - to name tables without using reserved keywords or to always use brackets around tables name
I'd assume IS is a reserved keyword. Try wrapping it around square brackets:
SELECT Table1.id, Table1.Name, Table1.Status, Table1.DateEntered FROM [IS].dbo.Table1 Table1