Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9160299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:42:21+00:00 2026-06-17T13:42:21+00:00

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

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:42:22+00:00Added an answer on June 17, 2026 at 1:42 pm

    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)
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: SQL Server Database query help Hi, I have a problem that I
Possible Duplicate: SQL Server: Get Top 1 of Each Group I have 2 tables
Possible Duplicate: Get script of SQL Server data I want to move some rows
Possible Duplicate: Distinct in SQL Server I want to select disctinct PokemonId variables but
Possible Duplicate: Sql server version problem when database restore I would like to restore
Possible Duplicate: Get substring in SQL Server Let's say I have MyImage.png or MyDoc.doc
Possible Duplicate: Most efficient way in SQL Server to get date from date+time? I
Possible Duplicate: SQL Server - use a parameter to select the top X of
Possible Duplicate: SQL Server query to find all current database names I am trying
Possible Duplicate: SQL Server 2008 Spatial: find a point in polygon I am working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.