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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:50:49+00:00 2026-06-15T14:50:49+00:00

Using SQL Server I am trying to inject a string into a SQL statement

  • 0

Using SQL Server I am trying to inject a string into a SQL statement based on an if statement, note that am trying to accomplish this inside a stored procedure.

I am currently getting an error for this code:

Declare @topString varchar(240)

IF @topRecords > 0
SET @topString = 'top 500'
ELSE
SET @topString = ''

SELECT @topString * FROM( //incorrect syntax near FROM

SELECT  top 500 c.Id as [Customer Id],....
    UNION
    SELECT  top 500 c.Id as [Customer Id],....
)as table1 
Order by 1 desc

Edit

if somethingTrue
@whereCondition = '1 = 1 '
else 
@whereCondition = branch = @branch

select * from table 
where @whereCondition AND etc...

Correct

for injection inside an if statement go with Jodrell
but if you need a dynamic top then go with what was suggested by Kaf.
thanks both for the help!

  • 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-15T14:50:50+00:00Added an answer on June 15, 2026 at 2:50 pm

    You can’t inject statement parts as variables like that, however you can change most values for parameters.

    Having a stored procedure perform operations that may require different query plans, based on a parameter is a bad idea, the results of this SP could vary wildly based on the value of the @topRecords parameter. You would need to use the RECOMPILE option to warn the query engine, mitigating much of the benefit of SPs. Have you considered just having two stored procedures?

    If you want to do it dynamically, you could build the whole statement dynamically, making one big string, then execute that.

    You should investigate using sp_executesql to execute the string/VarChar. Then similar queries will benefit from query plan reuse.

    As ever Sommarskog is a good reference.


    Something like this

    DECLARE @topString varchar(240);
    DECLARE @statement varchar(max);
    
    IF @topRecords > 0
        SET @topString = 'TOP 500';
    ELSE
        SET @topString = '';
    
    SET @statement = 'SELECT ' + @topString + ' * FROM
        (
            SELECT TOP 500 c.Id as [Customer Id], ....
            UNION
            SELECT TOP 500 c.Id as [Customer Id], ....
        ) table1 
        ORDER BY 1 DESC'
    
    /* Then execute @statement */
    EXEC sp_executesql @statement
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using SQL Server 2005 Analysis Services and I'm trying to calculate distance inside
I am trying to insert certain values into the table using SQL server 2005.
Using SQL Server 2008 Reporting services: I'm trying to write a report that displays
Using SQL Server 2008, I am trying to do something similar to this post
I'm using SQL Server 2005 and I'm trying to achieve something like this: I
Using SQL Server 2008 I'm trying to build up a string and execute it
I'm using SQL Server 2008. I'm trying to script out all my stored procedures,
I am using SQL Server 2008r2. Here is what I am trying to accomplish:
Using SQL Server 2000 I am trying to use this command in Query Analyzer
Using SQL Server 10.0 and 10.5 I'm trying to fetch multiple rows into one

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.