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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:41:04+00:00 2026-06-16T01:41:04+00:00

This is my sql code – declare @maxprice int –1 set @maxprice = 1000

  • 0

This is my sql code –

declare @maxprice int --1
set @maxprice = 1000 --2
select @maxprice --3

I can execute these combinations 1,2 and 1,2,3 without errors. But, when I execute 1,2 first and 3 later, i get the error i mentioned.

I am not able to understand why this happens. Shouldn’t the select @maxprice be similar to a regular select statement ?

  • 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-16T01:41:06+00:00Added an answer on June 16, 2026 at 1:41 am

    SQL statements like these are executed in as a block, and commands and definitions are only valid within the block. As soon as the block is finished, any declared variables go out of scope, and are undefined.

    So, when you execute just line 3, SQL server does not know or check if you have previously done something with the same variable, and naturally complains that the variable is not defined.

    If this was not so, and once declared variables remained in scope (e.g. for the duration of the session), the system would have to take great care to resolve clashes between, for example, @maxprice being declare with a int in one procedure, and with a money type in another.

    A more detailed discussion of variables and scoping rules in SQL Server, can be found here.

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

Sidebar

Related Questions

This SQL Server 2005 T-SQL code: DECLARE @Test1 varchar; SET @Test1 = 'dog'; DECLARE
in this SQL code DECLARE @n tinyint WHILE (@n > 0) BEGIN SELECT @n
I am running this sql code in SQL 2005 declare @a as float(24) set
I have this SQL code declare @s varchar(8000) = 'manoeuvre' select CHARINDEX(char(140), @s, 0)
I have this SQL code here.. SELECT cl.clientid, cl.clientname, cl.billingdate, cp.startdate, cp.expiration, (SELECT COUNT(*)
How do I rewrite this T-SQL code to produce the same results SELECT ACC.Title,
I use this code to select password field from the sql server 2012 db,
I got this SQL code in a SP: (MS SQL 2008) DECLARE @type tinyint
How can you fix this SQL-code? My Python code: import os, pg, sys, re,
So I want to do something like this sql code below: select s.id, s.name,s.city

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.