Re: Error msg
- From: Roy <Roy@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 15 Nov 2005 14:12:01 -0800
Erland,
I have corrected my stored procedures with what you said. But I still get
same error. Below is my stored procedure code:
CREATE PROCEDURE spAddTable1
@IID uniqueidentifier, @Name nchar(128)
AS
SET NOCOUNT ON
INSERT Table1 (IID, Name)
VALUES(@IID, @Name)
SET NOCOUNT OFF
GO
CREATE PROCEDURE spUpdateTable1
@IID uniqueidentifier, @Name nchar(128)
AS
SET NOCOUNT ON
UPDATE Table1
SET Name = @Name
WHERE IID = @IID
SET NOCOUNT OFF
GO
CREATE PROCEDURE spSetData
@IID uniqueidentifier, @Name nchar(128), @Num smallint
AS
SET NOCOUNT ON
DECLARE @return_status int
-- Check record, update if exist, add new if nonexist
IF NOT EXISTS(
SELECT *
FROM Table1 INNER JOIN Table2
ON Table1.IID = Table2.IID
WHERE Table1.IID = @IID
)
BEGIN
EXEC @return_status = spAddTable1 @IID, @Name
INSERT Table2 (IID, Num)
VALUES (@IID, @Num)
END
ELSE
BEGIN
EXEC @return_status = spUpdateTable1 @IID, @Name
UPDATE Table2
SET Num = @Num
WHERE IID = @IID
END
SET NOCOUNT OFF
I called spSetData procedured and it will in turn call spSetTable1. I
checked my code and found that this is actually my first procedure call after
I open the session. This procedure is in a transaction. Can you find any
error here?
Note: Table1 has two fields (IID and Name) and Table2 has two fields (IID
and Num) and there is a one-one relation between them.
Thanks.
"Erland Sommarskog" wrote:
> Roy (Roy@xxxxxxxxxxxxxxxxxxxxxxxxx) writes:
> > The stored procedures looks like this:
> > CREATE PROCEDURE sp
> > @ID int, @Parameter ...
> > AS
> > SET NOCOUNT ON
> > SELECT * FROM table
> > WHERE ID = @ID
> > IF (@@ROWCOUNT <> 0)
> > BEGIN
> > // update record table here
> > END
> > ELSE
> > BEGIN
> > // add record to table here
> > END
> > RETURN @@ERROR
> > GO
> >
> > These parameters are used to add/update records. Will they generate
> > unconsumed result sets as you said?
>
> "SELECT * FROM table" generates a result set which you need to process.
> But if all you want to do is to check for existence, you should do:
>
> IF NOT EXISTS (SELECT * FROM tbl WHERE id = @id)
> BEGIN
> INSERT ....
> END
> ELSE
> UPDATE ...
> END
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
>
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
.
- Follow-Ups:
- Re: Error msg
- From: Erland Sommarskog
- Re: Error msg
- References:
- Re: Error msg
- From: Erland Sommarskog
- Re: Error msg
- From: Erland Sommarskog
- Re: Error msg
- From: Erland Sommarskog
- Re: Error msg
- Prev by Date: Reg OLEDB images..
- Next by Date: Can anyone help me about this ERROR?Oledb
- Previous by thread: Re: Error msg
- Next by thread: Re: Error msg
- Index(es):
Relevant Pages
|