Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating Values in stored procedures

Posted on 2009-07-13
3
Medium Priority
?
257 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 2000 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
In this article I will describe the Copy Database Wizard 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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

636 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