Use an if then or a case statement in sql

I have a stored procedure that checks whether a table exists or not.

I then want to take the results and do a check on it, if it = 0 then insert the table, if it = 1 then do not insert the table.

The execution of the stp works fine.  I am getting an incorrect syntax near the word then when running the query.
declare @Results integer
exec sp_CheckIfTableExists 'ErpOdbc', @Results OUTPUT
Print 'The results of the sp_checkiftableexists was - ' + str(@Results)
if @Results = 0 then
begin 
	BEGIN TRANSACTION
	SET QUOTED_IDENTIFIER ON
	SET ARITHABORT ON
	SET NUMERIC_ROUNDABORT OFF
	SET CONCAT_NULL_YIELDS_NULL ON
	SET ANSI_NULLS ON
	SET ANSI_PADDING ON
	SET ANSI_WARNINGS ON
	COMMIT
	BEGIN TRANSACTION
	GO
	CREATE TABLE dbo.ErpOdbc
		(
		Interface nvarchar(MAX) NULL,
		DSN nvarchar(50) NULL,
		Server nvarchar(50) NULL,
		DatabaseName nvarchar(MAX) NULL,
		UserID nvarchar(50) NULL,
		Password nvarchar(50) NULL,
		Plant nvarchar(50) NULL
		)  ON [PRIMARY]
		TEXTIMAGE_ON [PRIMARY]
	GO
	COMMIT
	print '************************************************'
	print 'ErpOdbc table has been inserted successfully'
	print '************************************************'
end
else
begin
	print '************************************************'
	print 'ErpOdbc table has already been inserted'
	print '************************************************'
End
go

Open in new window

Bran-DamageAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Atdhe NuhiuCommented:
you don't need 'then'

if @Results = 0 
begin 
        BEGIN TRANSACTION
        SET QUOTED_IDENTIFIER ON
        SET ARITHABORT ON
        SET NUMERIC_ROUNDABORT OFF
        SET CONCAT_NULL_YIELDS_NULL ON
        SET ANSI_NULLS ON
        SET ANSI_PADDING ON
        SET ANSI_WARNINGS ON
        COMMIT
        BEGIN TRANSACTION
        GO
        CREATE TABLE dbo.ErpOdbc
                (
                Interface nvarchar(MAX) NULL,
                DSN nvarchar(50) NULL,
                Server nvarchar(50) NULL,
                DatabaseName nvarchar(MAX) NULL,
                UserID nvarchar(50) NULL,
                Password nvarchar(50) NULL,
                Plant nvarchar(50) NULL
                )  ON [PRIMARY]
                TEXTIMAGE_ON [PRIMARY]
        GO
        COMMIT
        print '************************************************'
        print 'ErpOdbc table has been inserted successfully'
        print '************************************************'
end
else
begin
        print '************************************************'
        print 'ErpOdbc table has already been inserted'
        print '************************************************'
End
go

Open in new window

0
Bran-DamageAuthor Commented:
If i run it without the end I get these results:
************************************************
Beginning ErpOdbc table insert
************************************************
Msg 102, Level 15, State 1, Line 15
Incorrect syntax near 'TRANSACTION'.
Msg 2714, Level 16, State 6, Line 1
There is already an object named 'ErpOdbc' in the database.
Msg 156, Level 15, State 1, Line 6
Incorrect syntax near the keyword 'else'.

also, I know that the results is going to = 1 in my test case.

If I run just this part:
declare @Results integer
exec sp_CheckIfTableExists 'ErpOdbc', @Results OUTPUT
Print 'The results of the sp_checkiftableexists was - ' + str(@Results)

I get a 1
0
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Atdhe NuhiuCommented:
>If i run it without the end I get these results:

I assume you meant without the 'then'

try taking the 'go's out
0
Atdhe NuhiuCommented:
this runs for me

declare @results as int
set @Results = 0
if @Results = 0 
begin 
        BEGIN TRANSACTION
        SET QUOTED_IDENTIFIER ON
        SET ARITHABORT ON
        SET NUMERIC_ROUNDABORT OFF
        SET CONCAT_NULL_YIELDS_NULL ON
        SET ANSI_NULLS ON
        SET ANSI_PADDING ON
        SET ANSI_WARNINGS ON
        COMMIT

        BEGIN TRANSACTION
        CREATE TABLE dbo.ErpOdbc
                (
                Interface nvarchar(MAX) NULL,
                DSN nvarchar(50) NULL,
                Server nvarchar(50) NULL,
                DatabaseName nvarchar(MAX) NULL,
                UserID nvarchar(50) NULL,
                Password nvarchar(50) NULL,
                Plant nvarchar(50) NULL
                )  ON [PRIMARY]
                TEXTIMAGE_ON [PRIMARY]
        COMMIT
        print '************************************************'
        print 'ErpOdbc table has been inserted successfully'
        print '************************************************'
end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Atdhe NuhiuCommented:
and so does this

declare @results as int
set @Results = 1
if @Results = 0 
begin 
        BEGIN TRANSACTION
        SET QUOTED_IDENTIFIER ON
        SET ARITHABORT ON
        SET NUMERIC_ROUNDABORT OFF
        SET CONCAT_NULL_YIELDS_NULL ON
        SET ANSI_NULLS ON
        SET ANSI_PADDING ON
        SET ANSI_WARNINGS ON
        COMMIT

        BEGIN TRANSACTION
        CREATE TABLE dbo.ErpOdbc
                (
                Interface nvarchar(MAX) NULL,
                DSN nvarchar(50) NULL,
                Server nvarchar(50) NULL,
                DatabaseName nvarchar(MAX) NULL,
                UserID nvarchar(50) NULL,
                Password nvarchar(50) NULL,
                Plant nvarchar(50) NULL
                )  ON [PRIMARY]
                TEXTIMAGE_ON [PRIMARY]
        COMMIT
        print '************************************************'
        print 'ErpOdbc table has been inserted successfully'
        print '************************************************'
end
else
begin
        print '************************************************'
        print 'ErpOdbc table has already been inserted'
        print '************************************************'
End

Open in new window

0
Bran-DamageAuthor Commented:
Excellent, that worked perfectly.

Thanks!
0
Atdhe NuhiuCommented:
thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.