473,800 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Local Variables In A Stored Procedure

23 New Member
Hey guys I need some help with local variables, here is my current code:

Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE CreateFile
  2.  
  3.  AS  
  4. DECLARE @notification int
  5. SET @notification = (SELECT NotificationPeriod FROM Notification Where NotificationID = 1)
  6. SET NOCOUNT ON
  7.  
  8. --Populate template
  9. EXEC sp_makewebtask 
  10.  
  11. @outputfile = 'C:\xpsmtp\Test1.html',
  12. @query = 'SELECT ContractSequenceNumber, ContractName, Vendor, OrigValue, CONVERT(VARCHAR, StartDate), CONVERT(VARCHAR, EndDate) FROM Contracts WHERE  EndDate >= CURRENT_TIMESTAMP AND EndDate <= CURRENT_TIMESTAMP + @notification',
  13. @templatefile = 'C:\xpsmtp\Test1.tpl',@dbname = ContractMgt, @whentype = 1
  14. GO
Now with the @notification local variable, it is being set to a value with the first select statement, this value is either 30, 60, or 90 and will be changed by the user periodically. Now the the procedure has this variable set to a value from pulling it in from a table, I want it to be used in the second query which is @query. It is supposed to select records between todays date and 30, 60, or 90 days from now, which would be where the @notification comes into play. When I try to execute this procedure I get this error:

Must declare the variable '@notification' .

Any ideas why it is not working? When I hardcode a a value in place of @notification (30, 60, or 90) the query works perfectly. Thanks very much in advance!
Jan 4 '07 #1
2 16421
iburyak
1,017 Recognized Expert Top Contributor
@notification can not be inside your quoted string in this case it is not used as a variable.
See my small correction below

[PHP]
@query = 'SELECT ContractSequenc eNumber, ContractName, Vendor, OrigValue, CONVERT(VARCHAR , StartDate),
CONVERT(VARCHAR , EndDate)
FROM Contracts WHERE EndDate >= CURRENT_TIMESTA MP
AND EndDate <= CURRENT_TIMESTA MP + ' + @notification[/PHP]
Jan 4 '07 #2
gderosa
23 New Member
Thanks very much, it works!
Jan 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
4381
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm stuck here.
8
431
by: pertheli | last post by:
I am in a situation where only "goto" seems to be the answer for my program logic where I have to retry calling some repeated functions. Can anybody help in the usage of goto and its effect in local variables, as shown in the stripped code below void MyClass:Process(){ int iMaxRetry = 100;
4
2593
by: hals_left | last post by:
How do you declare and then SELECT a value for a local variable within stored procedure, increment the value and then use in an Insert statement? Thanks Any sites that explain this syntax for SQL Server 2000? Thanks hals_left CREATE PROCEDURE . @QualRef tinyint,
8
1312
by: Steven Blair | last post by:
Hi, I have a system which processes 1000's of transactions per hour. Part of each transaction process invloves me calling a stored procdure which updates various tables. What I want to know is, is a Stored Procedure like a function in terms of performance, each time the SP is about to get called, the function will have memory allocated for it on the stack(including all variables required). So, this process would happen however many...
5
2460
by: Abhilash.k.m | last post by:
This is regarding the session management using Out of proc session management(SQL SERVER). Among the samples below which one is better to set the session? 1. There are 20 session variables and all of them are being stored into session and accessed from session and individual session object. Example: Session = "XYZ", Session=100, Session="NAME", etc.
6
9909
by: Peter Neumaier | last post by:
Hi, I am trying to select some data through a stored procedure and would like to store the result in a local access table. Is that possible? Can somebody provide an example? Thanks&regards! Peter
1
2435
by: ILCSP | last post by:
Hello, I'm trying to accomplish 3 things with one stored procedure. I'm trying to search for a record in table X, use the outcome of that search to insert another record in table Y and then exec another stored procedure and use the outcome of that stored procedure to update the record in table Y. I have this stored procedure (stA) CREATE PROCEDURE procstA (@SSNum varchar(9) = NULL) AS
5
1809
by: Pedro Vera | last post by:
I am helping somebody setup one of the asp.net starter kits. I converted it from sql express to sql server with no real issues, and I got it running local perfectly. On my first attempt to run it remotely I noticed that a lot of the queries had dbo. hardcoded and that SQL server on the remote server was not allowing my sql account access to these. I removed all mentions of dbo. and again it still runs local. I have posted it to the...
3
2400
by: enreil | last post by:
Hello, I'm relatively new to the world of Stored Procedures in SQL Server. This may be a silly question, but I've done some searching and haven't come up with any solid answers. Is is possible to assign the result of a SELECT statement e.g. SELECT TOP 1 Cust_Name FROM Customers to a local variable within my stored procedure? If so, is this a good practice, or is there a better approach? Thank you.
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10033
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9085
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.