SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Variable Problem
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

yellowman
Starting Member

USA
25 Posts

Posted - 11/12/2012 :  15:52:09  Show Profile  Reply with Quote

I keep trying to run the piece of code below on one of my databases and I am getting and error that says the following:

Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '8000'.

Any ideas? Its probably something simple that I am overlooking (like a missing tick somewhere).


DECLARE @Database varchar(100)
DECLARE @Command nvarchar(200)
SELECT @Database = 'Bank_PROD'
SELECT @Command = 'SELECT ' +@Database+ ' AS dbname,Left(SUBSTRING(dbo.program, PATINDEX(''%[0-9.-]%'', dbo.program), 8000), PATINDEX(''%[^0-9.-]%'', SUBSTRING(dbo.program, PATINDEX(''%[0-9.-]%'', dbo.program), 8000) + ''X'')-1) AS Schema_Version
FROM ' +@Database + '.dbo.scriptdef
WHERE (dbo.program LIKE ''%sub schema_Version%'')'
EXEC(@Command)

sunitabeck
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 11/12/2012 :  16:01:22  Show Profile  Reply with Quote
Your query does not have a FROM clause even though the SELECT clause refers to column names. You will see what I mean if you print out the dynamic SQL.
...
WHERE (dbo.program LIKE ''%sub schema_Version%'')'
PRINT @Command
EXEC(@Command)
Go to Top of Page

yellowman
Starting Member

USA
25 Posts

Posted - 11/12/2012 :  16:13:32  Show Profile  Reply with Quote
I forgot about the PRINT command for debugging......thanks.

Looks like this piece got truncated:

+ ''X'')-1) AS Schema_Version
FROM ' +@Database + '.dbo.scriptdef
WHERE (dbo.program LIKE ''%sub schema_Version%'')'

Does the + sign get some sort of special treatment when its a string literal?
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4613 Posts

Posted - 11/12/2012 :  16:47:31  Show Profile  Reply with Quote
You need to make your variable for the Command larger, if you select the LEN of the variable you will see that it is full (200):
DECLARE @Database varchar(100)
DECLARE @Command nvarchar(200)
SELECT @Database = 'Bank_PROD'
SELECT @Command = 'SELECT ' +@Database+ ' AS dbname,Left(SUBSTRING(dbo.program, PATINDEX(''%[0-9.-]%'', dbo.program), 8000), PATINDEX(''%[^0-9.-]%'', SUBSTRING(dbo.program, PATINDEX(''%[0-9.-]%'', dbo.program), 8000) + ''X'')-1) AS Schema_Version
FROM ' +@Database + '.dbo.scriptdef
WHERE (dbo.program LIKE ''%sub schema_Version%'')'

SELECT LEN(@Command)
PRINT(@Command)
Go to Top of Page

yellowman
Starting Member

USA
25 Posts

Posted - 11/12/2012 :  16:57:31  Show Profile  Reply with Quote
I can't believe I overlooked the most obvious. I didn't even think to look at that. Thanks so much for the help.
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000