Solved

Creating Values in stored procedures

Posted on 2009-07-13
3
243 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
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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 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