Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PRINT Statement

Status
Not open for further replies.

ShortyA

MIS
Feb 20, 2002
363
CH
Hi,
I am writing a stored procedure that checks for a value in a table calle W01, if it exists then the value is updated with one from a reference table. If the record does not exist then an insert is done with the record from a reference table.

I would like to capture the values of a couple of the fields if an insert is done. Is this something that the PRINT statement would allow me to do ? If yes, please could someone help me out with the syntax as I am struggling due to SQL not being one of my skills!

Thanks - ShortyA
 
Try something like this

DECLARE @Thought1 VARCHAR(Size)
DECLARE @Thought2 VARCHAR(Size)

SET @Thought1 = Yourfield
SET @Thought2 = Yourfield

PRINT ' The result is ' + @thought1 + @thought2
 
Cool..thanks for that it has helped a lot! One final query....

My IF EXISTS check never seems to go to the ELSE part and to my untrained eye it looks OK. Am I missing something key ?

Code:
If EXISTS
	(select myfield From mytable Where xyz=abc)
  Begin
	UPDATE mytable2
	SET myfield=xyz
  End

Else
  Begin
    	INSERT INTO mytable3
		SELECT * FROM mytable4
  End

I have stripped down the code and simplified it for the posting!

ShortyA
 
The basic layout of your code looks fine to me. However how do you expect it to reach the Else statment when you are always updating a different table and differen collum?! i.e: xyz will still be abc in table mytable.

Did you mean this?

Code:
If EXISTS
    (select myfield From mytable Where xyz=abc)
  Begin
    UPDATE mytable
    SET xyz=xyz
  End

Else
  Begin
        INSERT INTO mytable3
        SELECT * FROM mytable4
  End
thanks

-Gus
 
Hi Gus,
in trying to simplify my code I think I went too far. Both the UPDATE and INSERT are done on the same table. The IF EXISTS select is done on yet another table to see if a case exists

Thanks - ShortyA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top