if condition not working - sql

I want to show a message if record is not in the table.
I wrote this small piece of code, but it's giving me an error.
create procedure abc(#id varchar(25))
as
declare #msg1 as varchar(55)
begin
select id,fname,lname from student where id=#id
--up to this is working correctly
--for showing msg i write down this lince of code
if id=#id
select #msg=“correct record“
else
select #msg=“record not found“
end
end

Add an EXISTS check instead of selecting the records
IF EXISTS ( select 1 from student where id=#id)
BEGIN
SET #msg = 'correct record'
END
ELSE
BEGIN
SET #msg = 'incorrect record'
END
Are you trying to return the value of #msg? If yes, add SELECT #msg at the end.

Use single quotes not double. And check for any results with ##rowcount
create procedure abc(#id varchar(25))
as
begin
select id,fname,lname from student where id=#id
if ##rowcount > 0
select 'correct record' as msg
else
select 'record not found' as msg
end
end

Related

How to union multiple select statements while they are not near together?

I have an sql query which check for existence of some records, if those records exist rise error for them otherwise insert them to database. In my query as I need to return error messages for every record, I need to select some custom texts, problem is that they are showing as separate tables, not in one table, which I want (as I am calling this query from nodejs app and it returns an array for me so it only returns first table (error message) for me).
I searched and reach these two options:
1- Use UNION (which is not solving my case)
2- Insert all records in another table and then get all it's record (which isn't beautiful! :) )
DECLARE #errorCOUNT int
SET #errorCOUNT = 0
BEGIN TRANSACTION [Tran1]
IF EXISTS (SELECT * FROM Categories WHERE CategoryName = 'myCat1')
BEGIN
SELECT 'This is error for is = 4' As err
SET #errorCOUNT = #errorCOUNT + 1
END
ELSE
BEGIN
INSERT INTO Categories VALUES ('myCat1')
END
----------------------------
IF EXISTS (SELECT * FROM Categories WHERE CategoryName = 'myCat2')
BEGIN
SELECT 'This is error for is = 5' AS err
SET #errorCOUNT = #errorCOUNT + 1
END
ELSE
BEGIN
INSERT INTO Categories VALUES ('myCat2')
END
----------------------------
IF #errorCOUNT > 0
BEGIN
ROLLBACK TRANSACTION [Tran1]
END
ELSE
BEGIN
COMMIT TRANSACTION [Tran1]
END
As I mentioned I want all these select statements to be shown in one table so they return to my server as one array.
I just think it is good to mention that my query completes in a loop, so it may have different amount of IF...ELSE (between --- lines).
I hope I was clear. Thanks in advance.
Try this one, would work:
BEGIN TRANSACTION [Tran1]
DECLARE #err AS TABLE ( msg NVARCHAR(MAX) NOT NULL )
DECLARE #errorCOUNT AS INT = 0
IF EXISTS (SELECT * FROM Categories WHERE CategoryName = 'myCat1')
BEGIN
INSERT INTO #err (msg) VALUES ('This is error for is = 4')
SET #errorCOUNT = #errorCOUNT + 1
END
ELSE
BEGIN
INSERT INTO Categories VALUES ('myCat1')
END
IF EXISTS (SELECT * FROM Categories WHERE CategoryName = 'myCat2')
BEGIN
INSERT INTO #err (msg) VALUES ('This is error for is = 5')
SET #errorCOUNT = #errorCOUNT + 1
END
ELSE
BEGIN
INSERT INTO Categories VALUES ('myCat2')
END
IF #errorCOUNT > 0
BEGIN
SELECT * FROM #err
ROLLBACK TRANSACTION [Tran1]
END
ELSE
BEGIN
COMMIT TRANSACTION [Tran1]
END
I don't understand what you're really want to do there, but here is a tip using MERGE statement and OUTPUT clause maybe it's what you're after
DECLARE #T TABLE(CategoryName VARCHAR(45));
MERGE INTO T
USING (VALUES('MyCat1'), ('MyCat2')) TT(CategoryName)
ON T.CategoryName = TT.CategoryName -- Or <> instead of =
WHEN NOT MATCHED THEN
INSERT VALUES(TT.CategoryName)
OUTPUT TT.CategoryName INTO #T;
SELECT CASE WHEN CategoryName = 'MyCat1'
THEN 'This is error for is = 4'
WHEN CategoryName = 'MyCat2'
THEN 'This is error for is = 5'
END Res
FROM #T;
Also, I don't think you need to the #ErrorCount variable, since you already have ##ROWCOUNT which you can use it instead.
Here is a db<>fiddle where you can see how it's working.

SQL IF/ELSE Statement

I'm struggling to understand why the below SQL will not work. I've put a comment (----------Fine up to Here), which is where SQL Server will accept the code when I parse/save the Store proceedure.
The bit below that, it will not take. Still new to SQL so any help would be great.
The error I receive is, Incorrect syntax near the keyword 'ELSE'. The "ELSE" being the one under the comment I mentioned above.
What I also don't understand is, If I change the IF and the BEGIN round, SQL accepts it (below)? I thought ELSE IF was not possible.
----------Fine up to Here
ELSE
IF (#GrabTypeID = '')
BEGIN
****************************************
Code below
**************************************
IF (#GrabtypeID NOT IN (SELECT GRABTYPEID FROM Mytable) AND #GrabtypeID != '') OR
(#Variable1 NOT IN (SELECT ID FROM Mytable) AND #Variable1 !='')
BEGIN
SELECT #ErrorMessage ='The GrabTypeID or the ID is an invalid value'
RAISERROR (#ErrorMessage, 16, 1)
PRINT 'Invalid Parameters passed Through'
RETURN
END
ELSE
BEGIN
IF (#GrabtypeID ! ='')
TRUNCATE TABLE Datatable1
TRUNCATE TABLE Datatable2
INSERT Datatable1
SELECT * FROM Referencetable1
INSERT Datatable2
SELECT * FROM REFERENCETABLE2
END
----------Fine up to Here
ELSE
BEGIN
IF (#GrabTypeID = '')
TRUNCATE TABLE Datatable1
TRUNCATE TABLE Datatable2
INSERT Datatable1
SELECT * FROM REFERENCETABLE1 WHERE CATEGORY = 4
INSERT Datatable2
SELECT * FROM REFERENCETABLE2 WHERE CATEGORY = 4
END
GO
Your format is a little weird. You could make it work the way you have it, but I think it would be better to use this format:
IF expression
BEGIN
SELECT
END
ELSE IF expression
BEGIN
SELECT
END
ELSE IF expression
BEGIN
SELECT
END
Specifically, change this:
ELSE
BEGIN
IF (#GrabtypeID ! ='')
To this (in both places):
ELSE IF (#GrabtypeID ! ='')
BEGIN

SQL IF AND ELSE STATEMENT

I have been trying to execute this if and else statement. However every execution indicates error such as either functional error or syntax related errors.
CREATE trigger [dbo].[TRIAL]
on [dbo].[TBL1]
after INSERT
AS
BEGIN
SET NOCOUNT ON;
IF TBL1.NUMBER = TBL2.NUMBER THEN
insert into TBL3 (NAME,HEIGHT)
select NAME,HEIGHT
from TBL1,TBL2
ELSE
PRINT 'WRONG NUMBER'
end
Would you please be able to help me to correct this?
To expand a bit on Alex K's comment:
declare #Flag bit = 1;
-- ERROR: There is no THEN keyword.
if #Flag = 1 then
select 'A';
-- CORRECT: Omit THEN and this works as expected.
if #Flag = 1
select 'A';
-- ERROR: Only the first SELECT is associated with the IF, so the ELSE is unmatched.
if #Flag = 2
select 'B1';
select 'B2';
else
select 'C';
-- CORRECT: If each branch of the IF has only one statement, then this construction is okay.
if #Flag = 2
select 'B1';
else
select 'C';
-- CORRECT: If you want multiple statements in either branch of the IF, make them into a block using BEGIN/END.
if #Flag = 2
begin
select 'B1';
select 'B2';
end
else
select 'C';
-- CORRECT: You can also use BEGIN/END with single statements if you like.
if #Flag = 2
begin
select 'B1';
select 'B2';
end
else
begin
select 'C';
end

Stored Procedure that accepts all table fields and updates those values

I'm working through a couple practice questions and I've run across this problem, I keep getting an error when trying to execute the procedure that says
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Procedure'."
Can someone please help?
Write a procedure UpdateTitle that accepts all the Title table columns and will update the title with those values. Raise error messages for the following: The ISBN does not exist The Category and/or Publisher Codes are not valid.
Create PROCEDURE UpdateTitle (#ISBN char(10), #SuggestedPrice smallmoney,#NumberInStock smallint,#PublisherCode int,#CategoryCode int)
AS
BEGIN
IF #ISBN is null or #CategoryCode is null or #PublisherCode is null
BEGIN
RAISERROR ('ISBN,CategoryCode, or PublisherCode is not valid please enter valid data',16,1)
END
ELSE
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = #ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET SuggestedPrice = #SuggestedPrice
WHERE ISBN = #ISBN;
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = #ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET NumberInStock = #NumberInStock
WHERE ISBN = #ISBN;
END
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = #ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET PublisherCode = #PublisherCode
WHERE ISBN = #ISBN;
END
BEGIN
IF (SELECT COUNT(*) FROM Title WHERE ISBN = #ISBN) = 0
BEGIN
RAISERROR ('ISBN does not exist.',16,1)
END
ELSE
BEGIN
SELECT 'Table Sucessfully Updated.';
UPDATE Title
SET CategoryCode = #CategoryCode
WHERE ISBN = #ISBN;
END
END
END
END
END
END
END
GO
Then
Execute Procedure UpdateTitle #ISBN ='1021031040', #suggestedproce ='40' , #NumberInStock ='10', #PublisherCode = '200', #CategoryCode = '1'
Execute Procedure UpdateTitle ...
Should be:
EXEC dbo.UpdateTitle ...
Some other comments:
ISBN is no longer limited to 10 characters (this change happened in 2007, if you believe WikiPedia).
Always use the schema prefix when creating or referencing objects.
You only need to check that the ISBN is valid once. And you shouldn't do so using a count IMHO, especially since - presumably - that is the key and it could only ever return 0 or 1 anyway.
You shouldn't select "update successful" and then perform the update. You should make sure the update was successful before telling the user it was successful.
There is no reason to separate this out into multiple updates either.
Please be liberal with carriage returns, indenting and whitespace. The value in readability is worth the extra cost in typing it (since you only type it once, but you will read it multiple times).
Use RETURN; as an exit mechanism so that you don't have to nest IF and ELSE multiple times.
Always use SET NOCOUNT ON; at the beginning of your procedures.
You probably want to customize the message to tell the user which parameter(s) were invalid.
Oh yeah, and please future-proof your code by using semi-colons to terminate statements.
Here is a much more concise version that satisfies all of your requirements:
CREATE PROCEDURE dbo.UpdateTitle
#ISBN CHAR(10),
#SuggestedPrice SMALLMONEY,
#NumberInStock SMALLINT,
#PublisherCode INT,
#CategoryCode INT
AS
BEGIN
SET NOCOUNT ON;
DECLARE #msg VARCHAR(255);
IF #ISBN IS NULL OR #CategoryCode IS NULL OR #PublisherCode IS NULL
BEGIN
SELECT #msg = 'The following parameter(s) were invalid:'
+ CASE WHEN #ISBN IS NULL THEN ' #ISBN' ELSE '' END
+ CASE WHEN #CategoryCode IS NULL THEN ' #CategoryCode' ELSE '' END
+ CASE WHEN #PublisherCode IS NULL THEN ' #PublisherCode' ELSE '' END;
RAISERROR (#msg, 11, 1);
RETURN;
END
IF NOT EXISTS (SELECT 1 FROM dbo.Title WHERE ISBN = #ISBN)
BEGIN
SET #msg = 'ISBN %s does not exist.';
RAISERROR(#msg, 11, 1, #ISBN);
RETURN;
END
BEGIN TRY
UPDATE dbo.Title
SET SuggestedPrice = #SuggestedPrice,
NumberInStock = #NumberInStock,
PublisherCode = #PublisherCode,
CategoryCode = #CategoryCode
WHERE ISBN = #ISBN;
SELECT 'Update successful.';
END TRY
BEGIN CATCH
SET #msg = ERROR_MESSAGE();
RAISERROR(#msg, 11, 1);
END CATCH
END
GO

T-SQL error object exists when separated in if/else blocks

I get the error: Msg 2714, Level 16, State 1, Line 16
There is already an object named '#mytemptable' in the database.
There are ways around it, but wonder why this happens. Seems like SQL Server is verifying both blocks of the if/else statement?
declare #choice int
select #choice = 1
if #choice = 1
begin
select 'MyValue = 1' AS Pick into #my_temp_table
end
else
begin
select 'MyValue <> 1' AS Pick into #my_temp_table
end
select * from #my_temp_table
drop table #my_temp_table
If the tables have different names, it works. Or if I create the temp table and use Insert Into... statements that works as well.
See here: What is deferred name resolution and why do you need to care?
basically you need to ceate the table first
So what is happening is that beginning with SQL server 7 deferred name resolution was enabled for real tables but not for temporary tables. If you change the code to use a real table instead of a temporary table you won’t have any problem
Here is another way
declare #choice int
select #choice = 1
declare #Value varchar(100)
if #choice = 1
select #Value = 'MyValue = 1'
else
select #Value = 'MyValue <> 1'
select #Value AS Pick into #my_temp_table
select * from #my_temp_table
drop table #my_temp_table
Try this:
declare #choice int
select #choice = 1
CREATE TABLE #my_temp_table(
Pick varchar(25)
)
if #choice = 1
begin
INSERT INTO #my_temp_table
select 'MyValue = 1'
end
else
begin
INSERT INTO #my_temp_table
select 'MyValue <> 1'
end
select * from #temptable
drop table #temptable
EDIT Sorry, I see that you tried this and the question was WHY does this happen. It is because SQL Server parses the stored procedure when it is created and checks for naming conflicts.