473,386 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Stored Procedure Help For Join Query

16
Hello members,

This is the SQL SERVER 2005 stored procedure.


It contains the data from the two diff tables.
(1)Customer :
Here clpid and cardid are composite primary keys with the identity field.It has storeid as foreign key.
The cardid is to be autogenerated everytime as the new customer is inserted.

I have to put the validations for the
* joindt = currentdate/todaydate
* dob is not greater than today date
* anniverdt is not less than equal to dob (in terms of year)

(2)Store :
Here storeid is the primary key.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. ALTER PROCEDURE CustomerStoredProcedure
  4.  
  5. @cardid INT OUTPUT,
  6. @clpid INT OUTPUT,
  7. @storename varchar(20),
  8. @firstname varchar(20),
  9. @lastname varchar(20),
  10. @joindt datetime,
  11. @anniverdt datetime,
  12. @dob datetime,
  13.  
  14. @frmErrorMessage AS VARCHAR(256) OUTPUT,
  15. @RETURNVALUE AS INT OUTPUT,
  16. @RETURNID AS INT OUTPUT
  17. AS
  18.  
  19. DECLARE
  20.  
  21.   @storeid AS INT
  22.  
  23. BEGIN
  24. SET NOCOUNT ON
  25.  
  26.  
  27. --validation...
  28. IF ( @firstname IS NULL OR @firstname = '' ) 
  29.     BEGIN
  30.         SET  @RETURNVALUE = -9
  31.         SET  @frmErrorMessage = 'FName is empty'
  32.         RETURN -9                        
  33.     END 
  34. IF ( @lastname IS NULL OR @lastname = '' ) 
  35.     BEGIN
  36.         SET  @RETURNVALUE = -9
  37.         SET  @frmErrorMessage = 'LName is empty'
  38.         RETURN -9                        
  39.     END
  40.  
  41. if  @DOB<@AnniversaryDate
  42.                 begin
  43.                     Select @frmErrorMessage ="AnniversaryDate cannot be greater then DOB"
  44.                     return -9
  45.                 end 
  46.             if  @DOB=@AnniversaryDate
  47.                 begin
  48.                     Select @frmErrorMessage ="AnniversaryDate cannot be equal to DOB"
  49.                     return -9
  50.                 end 
  51.  
  52.             if  @DOB>=getdate()
  53.                 begin
  54.                     Select @frmErrorMessage ="DOB cannot be greater then Today's Date"
  55.                     return -9
  56.                 end          
  57. BEGIN            
  58. IF EXISTS (select * from storemaster where storename = @storename)                                  
  59.        select @storeid = storeid from storemaster where storename = @storename                                                                                     
  60.  
  61.  
  62. IF EXISTS ( SELECT firstname,lastname,joindt,anniverdt,dob,
  63.                  FROM customermaster WHERE clpid = @clpid and cardid=cardid) 
  64.     BEGIN
  65.         UPDATE customermaster
  66.         SET firstname = @firstname,
  67.             lastname = @lastname,
  68.             storeid=@storeid,
  69.             joindt=@joindt,    
  70.             anniverdt=@anniverdt,
  71.             dob=@dob,
  72.  
  73.     WHERE  clpid= @clpid and cardid=@cardid
  74.  
  75.         SET  @frmErrorMessage = 'Name and other information has been updated'
  76.         SET @RETURNVALUE = 2
  77.     END
  78. END    
  79.  
  80. BEGIN
  81.    IF EXISTS (select * from storemaster where storename = @storename)                                  
  82.        select @storeid = storeid from storemaster where storename = @storename                                                                                     
  83.  
  84.  
  85.   IF NOT EXISTS(Select firstname,lastname,address 
  86.             from customermaster
  87.             where firstname = @firstname,lastname=@lastname)
  88.         BEGIN
  89.            INSERT INTO customermaster (firstname,lastname,joindt,anniverdt,dob,introducedby)
  90.                VALUES     (@firstname,@lastname,@joindt,@anniverdt,@dob,@introducedby)
  91.            SELECT @clpid = SCOPE_IDENTITY()
  92.            SELECT @cardid= SCOPE_IDENTITY()
  93.         END
  94.    ELSE
  95.        BEGIN
  96.           Print 'Name already exists'  
  97.        END
  98.  
  99.         SET  @frmErrorMessage = ' Name and other information has been Inserted'
  100.         SET @RETURNVALUE = 1
  101. END
  102.  
  103. SET NOCOUNT OFF
  104. END
  105.  
  106.  
Pls can anyone checkout my stored procedure as i have created for the first time for the join stored procedure.
It gives me the error but i can't rectify it.
It is too big but can anyone help then it's fine....
I have mentioned how i created my above procedure.
Can anyone also help me to check or put the validations for the date..

Thanxs in advance...waiting for solution

Hope to get a suitable reply
May 5 '07 #1
0 1711

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

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
10
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
2
by: jaYPee | last post by:
I have an existing query from MS Access that I want to convert it to SQL Server Stored Proc. My problem is on how to convert the WHERE clause. This is the query from MS Access: SELECT...
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
3
by: Simon | last post by:
I have a problem using a stored procedure in C#. Im using SQL2000 and C# with web parts. Currently i put the entire sql query in the CommandText. Then it works correctly and gives the...
9
by: Frawls | last post by:
Hi I Am am having problems with a stored Procedure that i wrote. Basically whats happening is that the Stored procedure Runs fine when i EXECUTE it in SQL Query analyzer. But when i debug...
6
by: yin_n_yang74 | last post by:
I am trying to create a report in Crystal Reports (v 8.5). I have a stored procedure to pull data from two databases and parameters. There are multiple one-to-many relationships and the stored...
12
by: brwalias | last post by:
Hi, using .net 2 sql server 2005 Here is my situation: I'm passing a variable in the url from a selection on Page A and need to display the results on the Results page be based on that...
4
by: aCe | last post by:
hi all, i need to convert these simple PHP code into stored procedure : <?php $result = mssql_query( "SELECT whid, whcode FROM warehouse" ); while( $wh = mssql_fetch_object( $result ) ) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...

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.