-2

Possible Duplicate:
SQL Server: how to get a database name as a parameter in a stored procedure

In the below stored procedure I am passing the database name as a parameter, but currently for testing I am database name via string which is declare in the procedure.

Please advise as I get an error in SET syntax and it's not able to recognise the db name I am passing.

USE [smtpFetch]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER Procedure [dbo].[checkForUpdateV]
(
    @ctsCode Varchar(100)
)
As

BEGIN

Declare @dbName varchar(100)
Declare @stDB TABLE
(
change_version_state varchar(max),
change_version_status varchar(200),
uniqueid  varchar(20),
Country_code  varchar(15),
Research_date  varchar(15),
data_date  varchar(15),
Make  varchar(50),
Model  varchar(50),
Versions varchar(255),
Model_year varchar(15),
doors varchar(5),
body varchar(5)
);
DECLARE @number_days as INT
DECLARE @DATAD as nvarchar(max)
DECLARE @DATAD2 as varchar(max)
SET @number_days = 5
SET @ctsCode=@ctsCode
SET @dbName='SSCRUS_CS2002'
SET @DATAD =N'Delete from ' + @ctsCode
select @DATAD
--print @dbName;
set @DATAD2=@DATAD2
INSERT INTO @stdb
set @DATAD2 = "SELECT  
  STUFF(
              (     SELECT ', ' + st105.full_text 
                    FROM"  + @dbName + ".dbo.version v2 
                    JOIN " + @dbName + ".dbo.equipment e105
                    ON e105.vehicle_id = v2.vehicle_id AND e105.schema_id = 105 
                    JOIN " + @dbName + ".dbo.schema_text st105  
                    ON st105.schema_id = 105 AND st105.data_value = e105.data_value AND    st105.language_id = 19 
                    WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime, CONVERT(char(8), v2.id_103))) > - @number_days) AND 
                                v.vehicle_id = v2.vehicle_id
                    FOR XML PATH('')
              ),1,1,''
              ) AS Change_version_state,

  STUFF(
              (     SELECT ', ' + st106.full_text 
                    FROM " + @dbName + ".dbo.version v3 
                    JOIN " + @dbName + ".dbo.equipment e106
                    ON e106.vehicle_id = v3.vehicle_id AND e106.schema_id = 106 
                    JOIN " + @dbName + ".dbo.schema_text st106  
                    ON st106.schema_id = 106 AND st106.data_value = e106.data_value AND st106.language_id = 19 
                    WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime,  CONVERT(char(8), v3.id_103))) > - @number_days) AND 
                                v.vehicle_id = v3.vehicle_id
                    FOR XML PATH('')
              ),1,1,''
              ) AS Change_version_status,
  v.id_101 AS [Unique ID], 
  v.id_109 AS Country, 
  v.id_103 AS [Research Date], 
  v.id_104 AS [Data Date],
  v.id_128 AS Make, 
  v.id_129 AS Model, 
  v.id_131 AS Version, 
  v.id_108 AS [Model Year], 
  v.id_605 AS Doors, 
  v.id_606 AS Body

  FROM " + @dbName + ".dbo.version v   
  WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime, CONVERT(char(8), v.id_103))) >  - @number_days)
  ORDER BY [Unique ID], Change_version_status, Change_version_state"
  execute sp_executesql @DATAD2
  --select * from @stdb
  END

Above stored procedure gives an error

Msg 156, Level 15, State 1, Procedure checkForUpdateV, Line 41
Incorrect syntax near the keyword 'set'.

Msg 103, Level 15, State 4, Procedure checkForUpdateV, Line 45
The identifier that starts with '.dbo.equipment e105
ON e105.vehicle_id = v2.vehicle_id AND e105.schema_id = 105 
' is too long. Maximum length is 128.

Msg 103, Level 15, State 4, Procedure checkForUpdateV, Line 47
The identifier that starts with '.dbo.schema_text st105  
ON st105.schema_id = 105 AND st105.data_value = e105.data_value 
AND st105.lang' is too long. Maximum length is 128.

Msg 103, Level 15, State 4, Procedure checkForUpdateV, Line 58
The identifier that starts with '.dbo.equipment e106
ON e106.vehicle_id = v3.vehicle_id AND e106.schema_id = 106 
' is too long. Maximum length is 128.

Msg 103, Level 15, State 4, Procedure checkForUpdateV, Line 60
The identifier that starts with '.dbo.schema_text st106  
ON st106.schema_id = 106 AND st106.data_value = e106.data_value 
AND st106.lang' is too long. Maximum length is 128.

Msg 103, Level 15, State 4, Procedure checkForUpdateV, Line 78
The identifier that starts with '.dbo.version v   
WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime, 
CONVERT(char(8), v.id_103))) > - @number_days)
ORDER ' is too long. Maximum length is 128.
Community
  • 1
  • 1
Cortez Ninja
  • 97
  • 3
  • 16

2 Answers2

1

Your syntax has a few issues.

You can't do a SET operation as part of a an INSERT statement.

INSERT INTO @stdb
set @DATAD2 = "SELECT  

Don't use double quotes for dynamic SQL

set @DATAD2 = "SELECT  

should be

set @DATAD2 = 'SELECT 

Watch your spacing when using the + syntax:

FROM"  + @dbName + ".dbo.version v2 

should be

FROM '  + @dbName + '.dbo.version v2 
Stuart Ainsworth
  • 12,792
  • 41
  • 46
  • Brilliant. Thanks for prompt reply. got only one error now. Please help how I can handle ',' Msg 102, Level 15, State 1, Procedure checkForUpdateV, Line 44 Incorrect syntax near ','. – Cortez Ninja Jan 18 '13 at 16:18
  • Thanks for pointing me out, my query is compiled now however when I execute I get a below error (0 row(s) affected) Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1 Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. – Cortez Ninja Jan 18 '13 at 16:45
  • Thanks I make it working now, however I am expecting the output to be stored in @stdb tablevariable but now its not happening. Its returning blank – Cortez Ninja Jan 18 '13 at 16:56
0

Following code is parse "Command(s) completed successfully." on mu ssms. I have remove all the sysntext error from your code. Hope this help

USE [smtpFetch]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER Procedure [dbo].[checkForUpdateV]
(
    @ctsCode Varchar(100)
)
As

BEGIN

Declare @dbName varchar(100)
Declare @stDB TABLE
(
change_version_state varchar(max),
change_version_status varchar(200),
uniqueid  varchar(20),
Country_code  varchar(15),
Research_date  varchar(15),
data_date  varchar(15),
Make  varchar(50),
Model  varchar(50),
Versions varchar(255),
Model_year varchar(15),
doors varchar(5),
body varchar(5)
);
DECLARE @number_days as INT
DECLARE @DATAD as nvarchar(max)
DECLARE @DATAD2 as varchar(max)
SET @number_days = 5
SET @ctsCode=@ctsCode
SET @dbName='SSCRUS_CS2002'
SET @DATAD =N'Delete from ' + @ctsCode
select @DATAD
--print @dbName;
set @DATAD2=@DATAD2
--INSERT INTO @stdb
Select @DATAD2 = '  INSERT INTO @stdb SELECT  
  STUFF(
              (     SELECT '',''  st105.full_text 
                    FROM '  + @dbName + '.dbo.version v2 
                    JOIN ' + @dbName + '.dbo.equipment e105
                    ON e105.vehicle_id = v2.vehicle_id AND e105.schema_id = 105 
                    JOIN ' + @dbName + '.dbo.schema_text st105  
                    ON st105.schema_id = 105 AND st105.data_value = e105.data_value AND    st105.language_id = 19 
                    WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime, CONVERT(char(8), v2.id_103))) > - @number_days) AND 
                                v.vehicle_id = v2.vehicle_id
                    FOR XML PATH('')
              ),1,1,''
              ) AS Change_version_state,

  STUFF(
              (     SELECT '', '' + st106.full_text 
                    FROM ' + @dbName + '.dbo.version v3 
                    JOIN ' + @dbName + '.dbo.equipment e106
                    ON e106.vehicle_id = v3.vehicle_id AND e106.schema_id = 106 
                    JOIN ' + @dbName + '.dbo.schema_text st106  
                    ON st106.schema_id = 106 AND st106.data_value = e106.data_value AND st106.language_id = 19 
                    WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime,  CONVERT(char(8), v3.id_103))) > - @number_days) AND 
                                v.vehicle_id = v3.vehicle_id
                    FOR XML PATH('')
              ),1,1,''
              ) AS Change_version_status,
  v.id_101 AS [Unique ID], 
  v.id_109 AS Country, 
  v.id_103 AS [Research Date], 
  v.id_104 AS [Data Date],
  v.id_128 AS Make, 
  v.id_129 AS Model, 
  v.id_131 AS Version, 
  v.id_108 AS [Model Year], 
  v.id_605 AS Doors, 
  v.id_606 AS Body

  FROM ' + @dbName + '.dbo.version v   
  WHERE   (DATEDIFF(day, { fn NOW() }, CONVERT(datetime, CONVERT(char(8), v.id_103))) >  - @number_days)
  ORDER BY [Unique ID], Change_version_status, Change_version_state'
  execute sp_executesql @DATAD2
  --select * from @stdb
  END

-- Temp table

Create table #@stDB (
change_version_state varchar(max),
change_version_status varchar(200),
uniqueid  varchar(20),
Country_code  varchar(15),
Research_date  varchar(15),
data_date  varchar(15),
Make  varchar(50),
Model  varchar(50),
Versions varchar(255),
Model_year varchar(15),
doors varchar(5),
body varchar(5)
)
Hiten004
  • 2,425
  • 1
  • 22
  • 34
  • One more question, currently my insert into OUTPUT is going into @stdb is there a way I can direct this into the physical table....Thanks – Cortez Ninja Jan 18 '13 at 16:23
  • Thanks for the above modifed query, my query is compiled now however when I execute I get a below error (0 row(s) affected) Msg 214, Level 16, State 2, Procedure sp_executesql, Line 1 Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'. – – Cortez Ninja Jan 18 '13 at 16:45
  • Thanks I make it working now, however I am expecting the output to be stored in @stdb tablevariable but now its not happening. Its returning blank – – Cortez Ninja Jan 18 '13 at 16:56
  • Yes you can direct this into the physical table. glad that i can help. – Hiten004 Jan 18 '13 at 17:04
  • Thanks Hiten004, but currently the problem is not storing the output into @stdb table variable at all – Cortez Ninja Jan 18 '13 at 17:07
  • @Shoaib Suleman `code` select * from @stdb Order by change_version_state, change_version_status `code` change change_version_state, change_version_status columns with your desire column. – Hiten004 Jan 18 '13 at 17:12
  • Yes, I done the same thing. if you see in my stored procedure I've a line which I've commented out when I posted the question and the purpose was to give me the output. But its not actually storing at all. – Cortez Ninja Jan 18 '13 at 17:15
  • i would use temptable(#stdb ) instade of table variable(@stdb). replace it and try that. – Hiten004 Jan 18 '13 at 17:21
  • when iam using temptable its giving me an error stating invalid column name...thanks for your help so far – Cortez Ninja Jan 20 '13 at 16:57
  • where in the code "invalid col" error is coming? please provide the code? – Hiten004 Jan 20 '13 at 17:56
  • I've resolved the issue now, I was using temp table from outside '' but yes its fixed now and thank you very much for your guidance and help – Cortez Ninja Jan 22 '13 at 13:20