Solved

Creating Values in stored procedures

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql query 12 72
Read a Table and add those records into a existing parameter that was send from a SSRS report 11 54
sql query help 2 51
SQL Query with Sum and Detail rows 2 49
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now