What’s wrong with my SQL statement?
ALTER PROCEDURE prGetDocById
@Nbr varchar(100),
@Type uniqueidentifier,
@TotalRecord int output
AS
BEGIN
IF(@Type is null)
BEGIN
(
SELECT @TotalRecord = COUNT(Category) FROM Document where Nbr = @Nbr and
Type = (case when @Type IS not null then @Type else null end)
SELECT * from Document
)
end
else
begin
(
...
)
end
end
it gives me this error:
Msg 156, Level 15, State 1, Procedure prGetDocById, Line 12
Incorrect syntax near the keyword ‘SELECT’.
Msg 102, Level 15, State 1, Procedure prGetDocById, Line 13
Incorrect syntax near ‘)’.
Msg 102, Level 15, State 1, Procedure prGetDocById, Line 19
Incorrect syntax near ‘)’.
I want to start with IF statement and need to return the output too, so there will be two select statement in my statement.
If I write it without IF statement, everything is OK.
Your code should follow the structure:
There are curly braces in your second condition which aren’t legal and the parenthesis aren’t legal/necessary (at least not in MS SQL Server) in the first condition body other than in specific circumstances.
Parenthesis Usage
Specific Query
I don’t understand the logic here (the
CASEseems pointless), but I think this is syntactically correct. Note that I put[Type]in brackets as it is a reserved word.