Solved

Creating Values in stored procedures

Posted on 2009-07-13
3
247 Views
Last Modified: 2012-05-07
I would like to create values inside stored procedures and assign them to certain variables in the stored procedure.  how would I do that?
0
Comment
Question by:VBBRett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24843096
SELECT @Variable = 100

SELECT @Variabl2 = SomeCOlumn
FROM SomeTable
WHERE someCondition
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 24845993
or with aggregations:

select @sumval = sum(somefield) from tablename

or from a function

select @value = dbo.udf_functionname()

get current date

select @date = getdate()

get current user

select @currentuser = suser_sname()
0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 500 total points
ID: 25014100
The one thing missing from the above examples is the DECLARE statement to create the variable in the first place.

To set the value of the variables you can use either SET or SELECT and do have some advantages depending on how / where they are being used. For example, in the code below we could use the SELECT strategy to set a few variables from a select clause. It really depends on whether or not it is truly a scaler variable.

e.g.


Create Procedure usp_My_Procedure (@param int)
as
Begin
   declare @I int
   declare @J int
   declare @K int
   declare @C char(1)
   declare @D datetime
 
   set @I = @param
   set @J = @I * 100
 
   set @I = (select max(number) from master..spt_values where type = 'P')  
   select @K = max(number), @C = max(type) from master..spt_values where type = 'P'
 
   set @D = getdate() + @param
 
   select @param as [param], @I as [i], @J as [j], @K as [k], @C as [C], @D as [date]
 
end
GO
 
-- then to test it simply use the exec function and pass the parameter (in this case the value 4)
 
exec usp_My_Procedure 4

Open in new window

0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question