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
Related
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'.
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 have a procedure:
CREATE OR REPLACE PROCEDURE recommend_book(
in_ID_user IN number
)
IS
--zmienne
zmienna1 number(9,0);
BEGIN
SELECT COUNT(GENRE)
INTO zmienna1
FROM BOOKS
WHERE ID_BOOK IN(SELECT ID_BOOK
FROM SIGNATURES
WHERE SIGNATURE IN (SELECT SIGNATURE
FROM ORDERS
WHERE ID_READER=in_ID_user)
);
END;
/
I'm getting the error
PL-00428: An INTO clause is expected in this SELECT statement
Could you please help me identify what's missing/what's wrong here?
For other developers when see this question, it should be an answer:
As Frank says:
Are you 100% sure that the error you're seeing isn't from a previous compilation attempt? Your procedure looks perfectly fine.
Just restart your compiler.
I have a task to get some code which is working correctly on SQL Server 2012 to work on SQL Server 2008 R2 as well. I got this error:
Additional information: Incorrect syntax near '<'
When I try to run my code I found out that something is wrong in this line of my SQL code
ALTER TABLE [dbo].[WorkTimeEntries]
ADD [TimeFinishedForRuntime] AS ISNULL([TimeFinished],
IIF ([TimeStarted] < SYSUTCDATETIME(), [dbo].[udf_GetCurrentDateTimeOffsetInTimeZone](DATENAME(TZOFFSET, [TimeStarted])), [TimeStarted]));
I have read that in this cases took place some kind of error when people try to get date, but I'm not sure what's wrong in my case.
There was no IIF in SQL Server 2008R2.
Replace it with CASE
ALTER TABLE [dbo].[WorkTimeEntries] ADD [TimeFinishedForRuntime] AS ISNULL(
[TimeFinished],
CASE WHEN [TimeStarted] < SYSUTCDATETIME() THEN [dbo].[udf_GetCurrentDateTimeOffsetInTimeZone](DATENAME(TZOFFSET, [TimeStarted])) ELSE [TimeStarted] END);
Basically, this error occurs if you any syntactical error in your SQL code. Please reverify all your statement sequence.
Im getting trouble with a full text search query. First, my first try is not working because theres a syntax error close to replace (and I dont understand why) (if I do the replace outside the contains it works):
declare #what varchar(100) = 'word1 word2'
select *
from tablea
where
contains(column1, replace(#what, ' ', ' near '))
My second problem is about the NEAR and FORMS OF syntax. I tried:
'formsof(inflectional, "word1") near formsof(inflectional, "word2")'
But its not working. Theres a syntax error too, not in the replace thing, but in the query. Is it possible to use a replace over the #what variable (outside or inside the contains) to get the correct syntax?