We help IT Professionals succeed at work.
Get Started

Use an if then or a case statement in sql

252 Views
Last Modified: 2012-05-09
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

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 8 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE