|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
SCOPE_IDENTITY & Must declare variableI am using SQL Server 2005 I have a table CREATE TABLE dbo.testrun_reports ( RunID INT IDENTITY(1,1) PRIMARY KEY, RunDate SMALLDATETIME NOT NULL DEFAULT GETDATE(), db2dbreport VARCHAR(MAX), incexcreport VARCHAR(MAX) ); and stored procedure alter PROCEDURE dbo.Run_Start @RunID INT OUTPUT AS SET NOCOUNT ON; INSERT dbo.testrun_reports(db2dbreport,incexcreport) values ('Started','Started'); SELECT @RunID = SCOPE_IDENTITY(); When I try to create this Stored Procedure I am getting an error 'Must Declare Scalar variable @RunID' I have it as OUTPUT paramter right? What is the problem here. Please let me know. Thanks <mod***@gmail.com> wrote in message
Show quote news:1156527713.966706.50160@m73g2000cwd.googlegroups.com... No idea. It works fine for me:> Hi All > I am using SQL Server 2005 > > I have a table > > CREATE TABLE dbo.testrun_reports > ( > RunID INT IDENTITY(1,1) PRIMARY KEY, > RunDate SMALLDATETIME NOT NULL DEFAULT GETDATE(), > db2dbreport VARCHAR(MAX), > incexcreport VARCHAR(MAX) > ); > > and stored procedure > > alter PROCEDURE dbo.Run_Start > @RunID INT OUTPUT > AS > > SET NOCOUNT ON; > INSERT dbo.testrun_reports(db2dbreport,incexcreport) values > ('Started','Started'); > SELECT @RunID = SCOPE_IDENTITY(); > > When I try to create this Stored Procedure > I am getting an error 'Must Declare Scalar variable @RunID' > I have it as OUTPUT paramter right? > > What is the problem here. Please let me know. > create table testrun_reports ( id int identity primary key, db2dbreport varchar(10), incexcreport varchar(10) ) create PROCEDURE dbo.Run_Start @RunID INT OUTPUT AS SET NOCOUNT ON; INSERT dbo.testrun_reports(db2dbreport,incexcreport) values ('Started','Started'); SELECT @RunID = SCOPE_IDENTITY(); go declare @RunID int exec dbo.Run_Start @RunID OUTPUT select @RunID RunID David I noticed someone else also had same issue
http://msforums.ph/forums/135728/ShowPost.aspx select RunID = SCOPE_IDENTITY(); -- works select @RunID = SCOPE_IDENTITY(); -- does not work What is the difference between the two. Please let me know. <mod***@gmail.com> wrote in message
news:1156529797.705740.88730@i3g2000cwc.googlegroups.com... This is a simple select that returns a value aliased as RunID.>I noticed someone else also had same issue > http://msforums.ph/forums/135728/ShowPost.aspx > > select RunID = SCOPE_IDENTITY(); -- works > select @RunID = SCOPE_IDENTITY(); -- does not work This is a variable assignment.David The first selects a value and displays it as tabular data with a column
header of RunID. The second tries to assign a value to a variable. The variable must be declared in the same batch in which it is assigned. So by itself, with no variable declaration, the second will fail. -- Show quoteHTH Kalen Delaney, SQL Server MVP <mod***@gmail.com> wrote in message news:1156529797.705740.88730@i3g2000cwc.googlegroups.com... >I noticed someone else also had same issue > http://msforums.ph/forums/135728/ShowPost.aspx > > select RunID = SCOPE_IDENTITY(); -- works > select @RunID = SCOPE_IDENTITY(); -- does not work > > What is the difference between the two. > > Please let me know. > Then why is it failing, it is declared as output paramter. I just want
to know if something specific to my environment setup. Thanks <mod***@gmail.com> wrote in message
news:1156530678.610423.218160@p79g2000cwp.googlegroups.com... It's not failing. It works fine.> Then why is it failing, it is declared as output paramter. I just want > to know if something specific to my environment setup. > Thanks > David Maybe you're getting the error in the call to the procedure?
How are you calling it... are you declaring a variable to pass in as the actual output parameter? e.g. declare @myvar int exec dbo.Run_Start @myvar output -- Show quoteHTH Kalen Delaney, SQL Server MVP "David Browne" <davidbaxterbrowne no potted m***@hotmail.com> wrote in message news:eqyFIcHyGHA.4972@TK2MSFTNGP03.phx.gbl... > > <mod***@gmail.com> wrote in message > news:1156530678.610423.218160@p79g2000cwp.googlegroups.com... >> Then why is it failing, it is declared as output paramter. I just want >> to know if something specific to my environment setup. >> Thanks >> > > It's not failing. It works fine. > > David > It works fine for me.
-- Show quoteArnie Rowland, Ph.D. Westwood Consulting, Inc Most good judgment comes from experience. Most experience comes from bad judgment. - Anonymous <mod***@gmail.com> wrote in message news:1156527713.966706.50160@m73g2000cwd.googlegroups.com... > Hi All > I am using SQL Server 2005 > > I have a table > > CREATE TABLE dbo.testrun_reports > ( > RunID INT IDENTITY(1,1) PRIMARY KEY, > RunDate SMALLDATETIME NOT NULL DEFAULT GETDATE(), > db2dbreport VARCHAR(MAX), > incexcreport VARCHAR(MAX) > ); > > and stored procedure > > alter PROCEDURE dbo.Run_Start > @RunID INT OUTPUT > AS > > SET NOCOUNT ON; > INSERT dbo.testrun_reports(db2dbreport,incexcreport) values > ('Started','Started'); > SELECT @RunID = SCOPE_IDENTITY(); > > When I try to create this Stored Procedure > I am getting an error 'Must Declare Scalar variable @RunID' > I have it as OUTPUT paramter right? > > What is the problem here. Please let me know. > > Thanks > |
|||||||||||||||||||||||