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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Stored procedure

Author  Topic 

ilimax
Posting Yak Master

164 Posts

Posted - 2007-09-19 : 21:27:33
I want to make one procedure like this;
1.select value from Table1
2.Check received value by if statement (so there will be 2 options)
3.first option: check in Table2 do you have one value.
if I do not have that value in Table2,
insert new value in Table2
4.second option:insert value in Table3.

Does anybody have similar example. I have never made if statement in stored procedure

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-09-19 : 21:48:33
Stored procedures are no different than what you would type in inside Query Analyzer, scripts, or anywhere else that T-SQL is allowed.

CREATE PROC SomeProc
AS

DECLARE @var1 int

SELECT @var1 = Column1 FROM Table1

IF NOT EXISTS (SELECT * FROM Table2 WHERE Column1 = @var1)
INSERT INTO Table2 ...

...


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

ilimax
Posting Yak Master

164 Posts

Posted - 2007-09-19 : 22:15:31
This is close to. Thank you. I am thinking on VB way.
How I can handle this my "else" in T-SQL?

If @var1 = 1 then
IF NOT EXISTS (SELECT * FROM Table2 WHERE Column1 = @var1)
INSERT INTO Table2
Else
INSERT INTO Table3
End If
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-09-19 : 23:29:26
Do you have SQL Server Books Online? You should open it up and check out IF.

There is no END IF in T-SQL. You use BEGIN/END when you have more than one command in the IF/ELSE.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

ilimax
Posting Yak Master

164 Posts

Posted - 2007-09-19 : 23:30:56
Hej thanks man. I am at home and I do not have here Books Onlin. I will check that tomorrow.

Thank you,
Go to Top of Page

ilimax
Posting Yak Master

164 Posts

Posted - 2007-09-20 : 21:58:55
I have more questions that I could not find answers today.
How I can set string varible empty ...
Something like
SET @myvarible = ""

Also, how I can say Exit Stored Procedure...
When I check somethinng with If in the midle of stored procedture I want to Exit stored procedure if some value is returned in SELECT statement...
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-09-20 : 22:35:19
Looks OK, except you need to use single quotes, not double quotes:

SET @myvarible = ''

"Also, how I can say Exit Stored Procedure..."

RETURN

Kristen
Go to Top of Page
   

- Advertisement -