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 7869917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:27:51+00:00 2026-06-03T01:27:51+00:00

Query 1: SET @sql2 = ‘insert into TempReport select ID, max(TransactionTime),0 from ClubTransaction with

  • 0

Query 1:

  SET @sql2 = 'insert into TempReport   
  select ID, max(TransactionTime),0 from  ClubTransaction with (nolock)  
  where ClubcardID in (select ClubcardID from TempCC)   
  and ClubcardTransaction.OfferID  not in (119,120,121)  
  group by ClubcardID' 
  exec (@Sql2)

Query 2:

  delcare @OfferID varchar(50)
  set   OfferID='1,112,445,'  
  SET @sql2 = 'insert into TempReport   
  select ID, max(TransactionTime),0 from  ClubTransaction with (nolock)  
  where ClubcardID in (select ClubcardID from TempCC)   
  and ClubcardTransaction.OfferID not in (Select Item From dbo.fnSplit(@OfferID,'','')   
  group by ClubcardID'    
  exec (@Sql2)

Query 1 works fine. In query2 I am replacing with an variable de defined where I am passing to the function fnSplit where I split the values with comma separated.
I get an error message Must declare the scalar variable "@OfferID".
Please let me know where is the issue here.

  • 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-03T01:27:53+00:00Added an answer on June 3, 2026 at 1:27 am

    You have to put the value from outside and replace the ‘ by “:

    Query 2:

      declare @OfferID varchar(50)
      set   OfferID='1,112,445,'  
      SET @sql2 = 'insert into TempReport   
      select ID, max(TransactionTime),0 from  ClubTransaction with (nolock)  
      where ClubcardID in (select ClubcardID from TempCC)   
      and ClubcardTransaction.OfferID not in (Select Item From dbo.fnSplit(' + replace(convert(varchar(4000), @OfferID), '''', '''''') + ',"","")   
      group by ClubcardID'    
    
      exec (@Sql2)
    

    Another solution (and a better one) is to use sp_executesql


    Be aware that dynamic SQL is a way to make sql injections and you should avoid using it..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query : set IDENTITY_INSERT dbo.OtherData1 ON INSERT INTO OtherData1 (OtherDataID, EmployeeID,
I'm confounded. The following MySQL query: SET @a := 0; SELECT * FROM users;
I currently have this query set-up: SELECT topic.content_id, topic.title, image.location FROM mps_contents AS topic
I currently have this query set-up: SELECT topic.content_id, topic.title, image.location FROM mps_contents AS topic
I have such a query: UPDATE `user` SET mail='abc@abc' WHERE id = IF((SELECT admin
Running the following query SET @rownum := 0; SELECT customer_id, @rownum := @rownum +1
I have this query: SET @current_group = NULL; SET @current_count = 0; SELECT user_id,
I have problem with optimize this query: SET @SEARCH = dokumentalne; SELECT SQL_NO_CACHE `AA`.`version`
I've got a query set up that puts 28 random records from a database
I have the following sql query SELECT * FROM jos_jcalpro_events AS e LEFT JOIN

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.