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

  • SEARCH
  • Home
  • 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 8755467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:52:00+00:00 2026-06-13T13:52:00+00:00

I am trying to create a SQL script that will create a nice easy

  • 0

I am trying to create a SQL script that will create a nice easy to update script where another person only needs to update the constants involved and then run a query, however I an getting into trouble when using declared variables in sub selects.

I know this is because the declared variables are not in scope of the sub select but cant seem to nut out a workaround.

Here is the code currently:

DECLARE @_logdb varchar(30) = 'nb_tablename';
DECLARE @_sitedb varchar(30) = 'nb_tablename_site';
DECLARE @_backtodate varchar(30) = '2012-10-10';
DECLARE @_sitename varchar(50) = 'bikehaven';

UPDATE
    [@_sitedb].[dbo].[bike] 
SET [bikeStatus] = 'active' 
WHERE bikeID in (
    SELECT 
substring(
    [event], 
    CHARINDEX('bike ID' , [event]) + LEN('bike ID '),
    CHARINDEX(
        ')', 
        [event],
        CHARINDEX('bike ID' , [event])
    ) - (CHARINDEX('bike ID' , [event]) + LEN('bike ID '))
) as 'ID'
from [@_logdb].[dbo].[logs] 
where 
    user_name = 'john' AND 
    event_type = 'Deleted bike' AND 
    CHARINDEX('bike ID' , [event]) > 0 AND 
    date_time > @_backtodate AND 
    siteID = (
        SELECT id 
        from [@_logdb].[dbo].[sites] 
        WHERE site_name = @_sitename
    )
)

The two variables that are actually causing trouble are @_backtodate & @_sitename, but I also know if I substitute those variables with the real string I then get errors saying that [@_sitedb].[dbo].[bike] is also a problem.

Ive done a fair bit of googling, tried using temporary tables for the sub queries as well as defining the SQL as a variable itself and then running an EXEC command on it, but to no avail.

Edit: I’ve applied the change as directed by Andomar by pulling out the query itself into a declare variable and then running that, which was indeed the fix for one of the errors, however even with that fix I am still getting Must define scalar variable errors for @_back to date & @_sitename.

Solution: The change suggested by Alexander Fedorenko ended up giving me the last bit of the puzzle. Which was table names cannot be used as explicit replacements via parameters in dynamic SQL, where these work perfectly for actual variable values.

In Short:
– Make the query a string variable.
– Any table name replacements that need to occur you can add via string concatenations (meaning the output string needs to be a valid query)
– Pass any variable parameters for substitution to the stored procedure SP_executesql

The final working code is as specified by Alexander Fedorenko.

  • 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-13T13:52:01+00:00Added an answer on June 13, 2026 at 1:52 pm

    procedure sp_executesql allows use parameters. Using sp_executesql

    DECLARE @_logdb varchar(30) = 'nb_tablename';
    DECLARE @_sitedb varchar(30) = 'nb_tablename_site';
    DECLARE @_backtodate varchar(30) = '2012-10-10';
    DECLARE @_sitename varchar(50) = 'bikehaven';
    
    DECLARE @_SQL nvarchar(max)
    SET @_SQL = '
    UPDATE
        ' + @_sitedb + '.[dbo].[bike] 
    SET [bikeStatus] = ''active'' 
    WHERE bikeID in (
        SELECT 
    substring(
      [event],
    CHARINDEX(''bike ID'' , [event]) + LEN(''bike ID ''),
    CHARINDEX(
    '')'', 
    [event],
    CHARINDEX(''bike ID'' , [event])
    ) - (CHARINDEX(''bike ID'' , [event]) + LEN(''bike ID ''))
    ) as ''ID''
    from ' + @_logdb + '.[dbo].[logs]
    where 
    user_name = ''john'' AND 
    event_type = ''Deleted bike'' AND
    CHARINDEX(''bike ID'' , [event]) > 0 AND
    date_time > @_backtodate AND
    siteID = (
    SELECT id 
    from ' + @_logdb + '.[dbo].[sites]
    WHERE site_name = @_sitename
        )
    )'
    EXEC sp_executesql @_SQL, N'@_backtodate varchar(30), @_sitename varchar(50)', @_backtodate, @_sitename
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write an sql trigger that will update another table whenever
im trying to create a sql query, that will detect (possible) duplicate customers in
I'm trying to create a SQL Server script that applies some operations to all
I am trying to create a ruby script that loads 2 .sql files and
I'm trying to create an SQL script to automatize inserting of values into a
I am trying to create some script variables in T-SQL as follows: /* Deployment
i am trying to create sql procedure or function that need to find duplicate
I'm trying to create an SQL query in PHP to update a table. Is
I'm trying to create an SQL statement that joins two tables and returns a
I'm trying to create a release script that can be deployed on multiple databases,

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.