dbTalk Databases Forums  

stored proc

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss stored proc in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
crocboy25
 
Posts: n/a

Default stored proc - 07-07-2010 , 08:28 AM






Hello,

I have a short procedure which compiles fine. When the code is called
from a webpage(.net) it runs the first time it is executed. However on
the second time it throws an error saying "There is already an object
named 'strPKHoldUp' in the database." StrPKHold is a variable that I
declare in the code as seen below.... I have never had a problem
before with declaring a variable and having it "save or cache" in the
database so you can use it again. Any idea how to solve this? I tried
moving the DECLARE statement around a little bit but that didnt help.
I changed the variable name to something else and it works one time
but fails after that...

Thanks in advance...

USE [MITIGATION_REPORTING]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UploadPictureFile]
(
@MIT_FK INT,
@FILE_TYPE CHAR(1),
@inFILE VARBINARY(MAX),
@CRE_USER VARCHAR(10),
@CRE_DATE DATETIME,
@MOD_USER VARCHAR(10) = null,
@MOD_DATE DATETIME = null
--@kFileName bigint output
)
AS



BEGIN

DECLARE @strPKHoldUp int

-- PUT IN THE CODE TO SEE IF THE PK AND THE TYPE ALREADY EXIST. IF SO,
UPDATE. IF NOT THEN INSERT.
select MR_MITIGATION_UPLOADS_MITIGATION_FK into strPKHoldUp
from dbo.MR_MITIGATION_UPLOADS
where MR_MITIGATION_UPLOADS_MITIGATION_FK = @MIT_FK
and MR_MITIGATION_UPLOADS_TYPE = @FILE_TYPE;


-- if no record was found, meaning that one needs to be inserted
if @strPKHoldUp = 0

begin
insert into
DBO.MR_MITIGATION_UPLOADS(MR_MITIGATION_UPLOADS_MI TIGATION_FK,
MR_MITIGATION_UPLOADS_TYPE,
MR_MITIGATION_UPLOADS_FILE,
MR_MITIGATION_UPLOADS_CRE_USER,
MR_MITIGATION_UPLOADS_CRE_DATE,
MR_MITIGATION_UPLOADS_MOD_USER,
MR_MITIGATION_UPLOADS_MOD_DATE)
values (@MIT_FK,
@FILE_TYPE,
@inFILE,
@CRE_USER,
@CRE_DATE,
@MOD_USER,
@MOD_DATE)
--set @kFileName = @@IDENTITY
end


-- a record was found so we need to update the record
if @strPKHoldUp > 0

begin


update dbo.MR_MITIGATION_UPLOADS
set MR_MITIGATION_UPLOADS_FILE = @inFILE
where MR_MITIGATION_UPLOADS_MITIGATION_FK = @MIT_FK
and MR_MITIGATION_UPLOADS_TYPE = @FILE_TYPE;



end

END

Reply With Quote
  #2  
Old   
David Martin
 
Posts: n/a

Default Re: stored proc - 07-07-2010 , 09:39 AM






On 07/07/2010 02:28 PM, crocboy25 wrote:
Quote:
Hello,

I have a short procedure which compiles fine. When the code is called
from a webpage(.net) it runs the first time it is executed. However on
the second time it throws an error saying "There is already an object
named 'strPKHoldUp' in the database." StrPKHold is a variable that I
declare in the code as seen below....
'StrPKHold' is not a variable, it is a table that exists in the database.
If you put '@' before its name, it will be considered a variable:
select MR_MITIGATION_UPLOADS_MITIGATION_FK into @strPKHoldUp

--
David Martin

Reply With Quote
  #3  
Old   
Bob Barrows
 
Posts: n/a

Default Re: stored proc - 07-07-2010 , 10:11 AM



crocboy25 wrote:
Quote:
Hello,

I have a short procedure which compiles fine. When the code is called
from a webpage(.net) it runs the first time it is executed. However on
the second time it throws an error saying "There is already an object
named 'strPKHoldUp' in the database." StrPKHold is a variable that I
declare in the code as seen below.... I have never had a problem
before with declaring a variable and having it "save or cache" in the
database so you can use it again. Any idea how to solve this? I tried
moving the DECLARE statement around a little bit but that didnt help.
I changed the variable name to something else and it works one time
but fails after that...

DECLARE @strPKHoldUp int

-- PUT IN THE CODE TO SEE IF THE PK AND THE TYPE ALREADY EXIST. IF SO,
UPDATE. IF NOT THEN INSERT.
select MR_MITIGATION_UPLOADS_MITIGATION_FK into strPKHoldUp
....
if @strPKHoldUp = 0
As David says "into strPKHoldUp" does not assign a value to
"strPKHoldUp", it creates a table called "strPKHoldUp" and inserts the
rows generated by your sql statement into it. Your code seems to work
for you the first time because an int variable contains 0 when it is
declared, and you weren't testing the situation where the key already
exists, in which case, this code would have quickly failed for you.

However, he failed to correct your syntax: to assign a value to a scalar
variable, you need to use "=", not "into". Also, you need to avoid
unintended consequences if your sql statement returns more than one row.
When assigning a value to a scalar variable from a sql statement, you
need to make sure that the sql statement only returns a single row,
something like this:

SET @strPKHoldUp = (SELECT TOP 1 MR_MITIGATION_UPLOADS_MITIGATION_FK
.... )

Note: I've come to prefer this syntax when assigning a value to a single
variable, only using the "SELECT @variable=column_value FROM table"
syntax when assigning values from several columns in the same row to
several variables

--
HTH,
Bob Barrows

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.