Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help creating query/stored procedure

Author  Topic 

tiwas
Starting Member

37 Posts

Posted - 2009-12-28 : 07:46:37
Hi,

I have set up the following table as a test

CREATE TABLE [dbo].[test](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[case_id] [bigint] NOT NULL,
[comment] [varchar](50) NOT NULL
) ON [PRIMARY]


The idea here is that I should get a new Case ID if I just create a row with NULL as case_id. However, if I specify a previous Case ID I should just add a new row with the specified ID and the comment (just add a comment to an existing case).

Do I need to create two stored procedures, or is it possible using just one?

divyaram
Posting Yak Master

180 Posts

Posted - 2009-12-28 : 08:08:22
Hi tiwas,

its possible to do it in single SP you have to check the condition for case_id if it ther in the test table then command should be existing case else as its a new one

Regards,
Divya
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-01-02 : 02:25:35
something like

CREATE PROC testproc
@caseid bigint,
@Comment varchar(50)
AS

UPDATE table
SET comment=@Comment
WHERE case_id=@caseid
IF @@ROWCOUNT =0
BEGIN
INSERT INTO table(case_id,comment)
VALUES(@Caseid,@comment)
END
GO
Go to Top of Page
   

- Advertisement -