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.
| Author |
Topic |
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-18 : 14:48:32
|
| Can someone show me a quick example how I can run a SP and it will then search for a record... if no record is found it will add it and if it is found it would update the record? Here is a dirty version which I know is not correct.. I just dont know how to handle the variables that are returned.-- FIRST LETS SELECT TO MAKE SURE ITS NOT A DUPECREATE procedure usp_InsertFeedData (@FeedID int, @FeedDataTitle nvarchar(300), @FeedDataLink nvarchar(300), @FeedDataDesc text, @FeedDataDate datetime)as Select Data_ID FROM RSS_Feeds_Data WHERE Data_Desc_Checksum = binary_checksum(substring(@FeedDesc,1,8000)) tempData_ID = Data_ID ' # LETS SEE IF IT FOUND ANY MATCHING RESULTS If tempData_ID<>"" Then -- LETS UPDATE THIS RECORD UPDATE RSS_Feeds_Data SET Data_DateCreated = Date WHERE Data_ID = tempData_ID Else -- LETS ADD THIS RECORD INSERT INTO RSS_Feeds_Data (Data_FeedID, Data_Title, Data_Link, Data_Desc, Data_DateCreated) VALUES (@FeedID, @FeedDataTitle, @FeedDataLink, @FeedDataDesc, @FeedDataDate) End IfGOQuality NT Web Hosting & Design |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-18 : 15:03:37
|
| one way is to not check for existance, just perform an update assuming it's there. If @@rowcount = 0 then you know it didn't exist, so add it.EDIT:eeww, I just read your code snippet. Is the only way you can check for existance is to check for the binary_checksum of a text column?How many rows in the table? Oh, I see, you have a column with the checksum already captured (indexed I assume). That's not too bad, although isn't it mathmatically possible to have false dupes?Be One with the OptimizerTG |
 |
|
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-18 : 15:30:26
|
| TG...Yes it is mathmatically possible to have false dupes but I would rather be safe then sorry. There is currently 4 million records in the database and trying to compare the text was taking the query way to long with all of the comparing going on in the table to make sure there was not any dupes before it inserted.Does this look correct? Sorry about it being so long but only way I thought it would make it readable. A example I looked at does not show a then in the if then statement so not sure if that is right or not.-- WE WILL UPDATE THE RECORD AND THEN CHECK TO SEE HOW MANY ITEMS WERE UPDATEDUPDATE RSS_Feeds_Data SET Data_DateCreated = Date WHERE Data_Desc_Checksum = binary_checksum(substring(@FeedDesc,1,8000)) AND Data_Title_Checksum = binary_checksum(substring(@FeedTitle,1,4000))if @@rowcount = 0 -- LETS ADD THIS RECORD INSERT INTO RSS_Feeds_Data (Data_FeedID, Data_Title, Data_Link, Data_Desc, Data_DateCreated) VALUES (@FeedID, @FeedDataTitle, @FeedDataLink, @FeedDataDesc, @FeedDataDate)else -- LETS MAKE SURE THERE IS NOT MORE THEN ONE RECORD if @@rowcount > 1 -- LETS DELETE SINCE THERE IS MORE THEN 1 RECORD UPDATED DELETE FROM RSS_Feeds_Data WHERE Data_Desc_Checksum = binary_checksum(substring(@FeedDataDesc,1,8000)) AND Data_Title_Checksum = binary_checksum(substring(@FeedTitle,1,4000)) -- LETS ADD THIS RECORD INSERT INTO RSS_Feeds_Data (Data_FeedID, Data_Title, Data_Link, Data_Desc, Data_DateCreated) VALUES (@FeedID, @FeedDataTitle, @FeedDataLink, @FeedDataDesc, @FeedDataDate) else -- WE ARE NOT GOING TO DO ANYTHING SINCE THERE IS ONLY 1 RECORD THAT WAS UPDATED end ifend ifQuality NT Web Hosting & Design |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-18 : 15:53:04
|
Looks reasonalbe. There were a couple of syntax errors and some your variable names weren't consistant (ie: @FeedDesc vs @FeedDataDesc) For If/Else statements you need either Begin/End blocks or only single statements for each condition. I changed some formatting just my reading pleasure. Finally, you cant have an empty else/end block. You need to have some statement in there or else sql will complain. So I threw in a raiserror level 10 (informational)Actually, I would think you want your app to know whether no action was necessary. You could use a raiserror like I put in there or you could return different ReturnCodes depending on which path was taken and your app can notify the user accordingly.Anyway, this will run syntactically:--I added these just to test syntaxdeclare @FeedID int ,@FeedDataTitle int ,@FeedDataLink int ,@FeedDataDesc int ,@FeedDataDate int-- WE WILL UPDATE THE RECORD AND THEN CHECK TO SEE HOW MANY ITEMS WERE UPDATEDUPDATE RSS_Feeds_Data SET Data_DateCreated = Date WHERE Data_Desc_Checksum = binary_checksum(substring(@FeedDataDesc,1,8000)) AND Data_Title_Checksum = binary_checksum(substring(@FeedDataTitle,1,4000))if @@rowcount = 0begin -- LETS ADD THIS RECORD INSERT INTO RSS_Feeds_Data (Data_FeedID ,Data_Title ,Data_Link ,Data_Desc ,Data_DateCreated) VALUES (@FeedID ,@FeedDataTitle ,@FeedDataLink ,@FeedDataDesc ,@FeedDataDate)endelsebegin -- LETS MAKE SURE THERE IS NOT MORE THEN ONE RECORD if @@rowcount > 1 begin -- LETS DELETE SINCE THERE IS MORE THEN 1 RECORD UPDATED DELETE FROM RSS_Feeds_Data WHERE Data_Desc_Checksum = binary_checksum(substring(@FeedDataDesc,1,8000)) AND Data_Title_Checksum = binary_checksum(substring(@FeedDataTitle,1,4000)) -- LETS ADD THIS RECORD INSERT INTO RSS_Feeds_Data (Data_FeedID ,Data_Title ,Data_Link ,Data_Desc ,Data_DateCreated) VALUES (@FeedID ,@FeedDataTitle ,@FeedDataLink ,@FeedDataDesc ,@FeedDataDate) end else begin -- WE ARE NOT GOING TO DO ANYTHING SINCE THERE IS ONLY 1 RECORD THAT WAS UPDATED raiserror('Record already exists', 10, 1) endendBe One with the OptimizerTG |
 |
|
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-18 : 16:29:52
|
| TG... thanks that seems to be exactly what I was looking for. I am having one problem though. I get this error...INSERT failed because the following SET options have incorrect settings: 'ANSI_NULLS., QUOTED_IDENTIFIER'.Quality NT Web Hosting & Design |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-18 : 16:46:59
|
| Sounds like you have an index on a calculated column or perhaps an indexed view? If so...before your sp code, set these to ON:ANSI_NULLSANSI_PADDINGANSI_WARNINGSARITHABORTCONCAT_NULL_YIELDS_NULLQUOTED_IDENTIFIER and this to off:NUMERIC_ROUNDABORTEDIT:one big pain with indexes on calculated columns is that when you want to update the value, the connection that is calling your sp also needs these set options set. From your application, you may need to set these after you open the connection and before you call the sp. Good luckBe One with the OptimizerTG |
 |
|
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-18 : 17:16:46
|
| TG,Thanks for being so helpful. I am trying to use Query Analyzer and not able to get it to work. I even put inside the SP all the options above to on except the roundabort to off and it did not work. Testing I am calling this from a ASP page also with passing items before the SP and still no go. I am so lost. I was told before that using caculations would be in a pain in the ____ but now I see why. The original query only had a SET ARITHABORT ON and it worked fine but I just cant seem to get this one to work.Quality NT Web Hosting & Design |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-18 : 17:24:53
|
| >>I even put inside the SP all the options above they need to be set before the sp codeie:set <options>...gocreate proc ...also, check if the table options are set correctly:--check your table settingsSElect objectproperty(object_id('RSS_Feeds_Data'), 'IsAnsiNullsOn')SElect objectproperty(object_id('RSS_Feeds_Data'), 'IsQuotedIdentOn')Be One with the OptimizerTG |
 |
|
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-18 : 17:48:28
|
| I was using the SET options here which I think is right..--insert spCREATE procedure usp_InsertFeedData_New (@FeedID int, @FeedDataTitle nvarchar(300), .........)as SET ANSI_NULLS ON ETC ETC -- WE WILL UPDATE THE WERE UPDATED UPDATE RSS_Feeds_Data SET Both of the IsAnsiNullsOn & IsQuotedIdentOn are both set at 1.Quality NT Web Hosting & Design |
 |
|
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-18 : 19:23:27
|
What is very strange is testing some things out in my ASP Page. I am running the following ASP code to run the SP..strSQLString = strSQLString & "SET ARITHABORT ON" & vbCrLfstrSQLString = strSQLString & "EXEC usp_InsertFeedData 1, 'Brad', 'http://www.brad.com/default.xml', 'Brads test', '6/30/2005 11:45:32 PM';"my_Conn.Execute (strSQLString) This works perfectly fine with the old SP that only has a insert inside it but does not SET anything inside the SP. The one that TG helped me right above both with and without any SET's inside the SP give errors. The only thing different is the select and update. Anyone have any ideas?Quality NT Web Hosting & Design |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-07-18 : 22:25:06
|
>>I was using the SET options here which I think is right..I think that's wrong (should be above the "create proc" statement)from BOL:quote: Stored procedures execute with the SET settings specified at execute time except for SET ANSI_NULLS and SET QUOTED_IDENTIFIER. Stored procedures specifying SET ANSI_NULLS or SET QUOTED_IDENTIFIER use the setting specified at stored procedure creation time. If used inside a stored procedure, any SET setting is ignored.
Try the statements from your SP individually from a QA window and see which ones you give you errors.can you run either of these from a QA window without errors? If not, what are the errors?set ANSI_NULLS onset ANSI_PADDING onset ANSI_WARNINGS onset ARITHABORT onset CONCAT_NULL_YIELDS_NULL onset QUOTED_IDENTIFIER onset NUMERIC_ROUNDABORT off--The insert statement...--Or this?UPDATE RSS_Feeds_Data SET Data_DateCreated = '6/30/2005 11:45:32 PM' WHERE Data_FeedID = 1--OR This?UPDATE RSS_Feeds_Data SET Data_DateCreated = Date WHERE Data_Desc_Checksum = binary_checksum(substring('Brads test',1,8000)) AND Data_Title_Checksum = binary_checksum(substring('Brad',1,4000))Be One with the OptimizerTG |
 |
|
|
redbrad0
Posting Yak Master
176 Posts |
Posted - 2005-07-19 : 00:07:36
|
| I think I am going to try and remove this math function. The records are never updated except for the date so I can calculate it once and then not have to worry about it again. Thanks for your help will update this ticket after I get things changed around.Quality NT Web Hosting & Design |
 |
|
|
|
|
|
|
|