Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exists Statement Error

Status
Not open for further replies.

vbjohn

Programmer
Aug 23, 2001
67
0
0
US
Can anyone help me figure this out? Why can I not use this variable.

I get this error for the second one I try: Server: Msg 170, Level 15, State 1, Line 31
Line 31: Incorrect syntax near '@NewICDB_NAME'.


DECLARE @curName VARCHAR(200)
DECLARE @NewICDB_NAME VARCHAR(200)

SET @curName = "Master" <-- I just hardcoded this for this example. This usually varies.

SET @NewICDB_NAME = 'SELECT * FROM ' + @curName + '.dbo.IC0001'

--> Error here
I tried ->IF Not EXISTS (EXEC(@NewICDB_NAME))
I tried ->IF Not EXISTS (@NewICDB_NAME)

BEGIN
SELECT 'The column does not exist'
END
ELSE BEGIN
SELECT 'The column exists'
END
 
If you know the name of the database, you could do this...

Code:
If Exists(Select 1 From Information_Schema.Tables Where Table_Name='MyTableName')
BEGIN
SELECT 'The column does not exist' 
END
ELSE BEGIN
SELECT 'The column exists'
END

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top