|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Why does the stored procedure behave differently?Following is a code snippet that can be run directly from the Query Analyzer. I am just trying to get my feet wet and I don't know why the stored procedure: sp_test does not correctly assign the correct value to @DataDesc. DROP TABLE #test_table CREATE TABLE #test_table ( guidcolumn uniqueidentifier default (newid()), datadesc varchar(15) null) GO INSERT INTO #test_table (datadesc) VALUES ('Hello') IF EXISTS (SELECT name FROM sysobjects WHERE name = N'sp_test' AND type = 'P') DROP PROCEDURE sp_test GO CREATE PROCEDURE sp_test @guidcolumn uniqueidentifier, @datadesc varchar(15) OUTPUT AS SELECT @datadesc = datadesc FROM #test_table WHERE guidcolumn = @guidcolumn GO -- Test goes here. DECLARE @guidcolumn uniqueidentifier DECLARE @datadesc varchar(15) SELECT @guidcolumn=guidcolumn FROM #test_table EXEC sp_test @guidcolumn, @datadesc SELECT @datadesc -- has null value SELECT @datadesc=datadesc FROM #test_table WHERE guidcolumn=@guidcolumn SELECT @datadesc -- has the correct value GO Amil,
If you run the following: DECLARE @guidcolumn uniqueidentifier DECLARE @datadesc varchar(15) SELECT @guidcolumn=guidcolumn FROM #test_table EXEC sp_test @guidcolumn, @datadesc Then the return value is displayed. The results you are getting is because you are selecting an empty variable. Hope this helps Barry "Barry" wrote: I don't quite understand what you mean. I was expecting that after the EXEC > Amil, > > If you run the following: > > DECLARE @guidcolumn uniqueidentifier > DECLARE @datadesc varchar(15) > SELECT @guidcolumn=guidcolumn FROM #test_table > EXEC sp_test @guidcolumn, @datadesc command, @datadesc would now have a value since the second parameter is an OUTPUT parameter. Am I missing something here? Show quote > > Then the return value is displayed. > > The results you are getting is because you are selecting an empty > variable. > > Hope this helps > > Barry > > > command, @datadesc would now have a value since the But you didn't specify it as such.> second parameter is an OUTPUT parameter. "Aaron Bertrand [SQL Server MVP]" wrote: If you read my original post, you may notice that I am still trying to > > command, @datadesc would now have a value since the > > second parameter is an OUTPUT parameter. > > But you didn't specify it as such. > learn. And I learned from Alejandro, constructively, that I should include the OUTPUT keyword when executing the stored proc. Show quote > > If you read my original post, you may notice that I am Yes, I did notice that.> still trying to learn. > And I learned from Alejandro, constructively, that I Telling you that you didn't specify it as such is not constructive? And > should include the OUTPUT keyword when executing > the stored proc. does not help you learn? Okay, I'll be sure to avoid answering your questions from here on out. Have you ever heard the phrases, "don't look a gift horse in the mouth," "don't shoot the messenger," "RTFM," or "don't sh*t where you eat"? > EXEC sp_test @guidcolumn, @datadesc EXEC sp_test @guidcolumn, @datadesc OUTPUTAMB Show quote "Amil" wrote: > Hi all, > Following is a code snippet that can be run directly from the Query > Analyzer. I am just trying to get my feet wet and I don't know why the > stored procedure: sp_test does not correctly assign the correct value to > @DataDesc. > > DROP TABLE #test_table > CREATE TABLE #test_table ( > guidcolumn uniqueidentifier default (newid()), > datadesc varchar(15) null) > GO > INSERT INTO #test_table (datadesc) VALUES ('Hello') > IF EXISTS (SELECT name > FROM sysobjects > WHERE name = N'sp_test' > AND type = 'P') > DROP PROCEDURE sp_test > GO > CREATE PROCEDURE sp_test > @guidcolumn uniqueidentifier, > @datadesc varchar(15) OUTPUT > AS > SELECT > @datadesc = datadesc > FROM #test_table > WHERE guidcolumn = @guidcolumn > GO > -- Test goes here. > DECLARE @guidcolumn uniqueidentifier > DECLARE @datadesc varchar(15) > SELECT @guidcolumn=guidcolumn FROM #test_table > EXEC sp_test @guidcolumn, @datadesc > SELECT @datadesc -- has null value > SELECT @datadesc=datadesc FROM #test_table WHERE guidcolumn=@guidcolumn > SELECT @datadesc -- has the correct value > GO works wonders. thanks.
Show quote "Alejandro Mesa" wrote: > > EXEC sp_test @guidcolumn, @datadesc > > EXEC sp_test @guidcolumn, @datadesc OUTPUT |
|||||||||||||||||||||||