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 |
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-08-27 : 11:28:17
|
| It's friday afternoon again! I have two extra days off because of the bank holiday and my beer fridge is calling me! Despite all this I still need to solve an SQL problem, which I can't help but feel is one that has at least in part been solved elsewhere.I have a table which holds various parameters - this is a fixed table as provided by our supplier. In this table each parameter is stored in a seperate field and so there is only a single record in the table (I daren't think what would happen if someone put another record in!!). In addition our illustrious supplier has reinvented field level locking!!! (and has never heard of Stored proc's, views etc.)The fields are (with sample values) NEXT_PTID PTID_LOCK NEXT_PDID PDID_LOCK NEXT_CLID CLID_LOCK12345 ABC 5678 235689 So in this case, the next PTID to be allocated would be 12345, the next PDID would be 5678 etc. Also in this case the PTID is 'locked' (which loosely translates as awaiting someone to hit the OK button). I know this because the value in PTID_LOCK is not null, if it was null as in PDID_LOCK and CLID_LOCK then it would not be locked.The general process is:-IF ..._LOCK is Null THEN INSERT (some data) into the ..._LOCK field SELECT NEXT..._ID Increment the value in NEXT..._ID INSERT NULL into the ..._LOCK fieldWhich is pretty straightforward and I can do that. I do however have two problems. Firstly the else clause which needs to wait (preferably for a finite and fixed time) before trying again and eventually giving up (i.e. a timeout) if the field is still locked. How on earth do I do this in SQL?Secondly, rather than writing three seperate stored procedures that essentially do the same thing it occured to me that I should be using Dynamic SQL somehow. I couldn't find anything elsewhere that tells me how to do what I want even though I swear I have seen it in these boards somewhere.Does anyone have any suggestions on where to look to get more ideas/assistance on these?Many thankssteveSteve no function beer well without |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-27 : 11:32:02
|
quote: Transact-SQL Reference WAITFORSpecifies a time, time interval, or event that triggers the execution of a statement block, stored procedure, or transaction.SyntaxWAITFOR { DELAY 'time' | TIME 'time' }ArgumentsDELAYInstructs Microsoft® SQL Server™ to wait until the specified amount of time has passed, up to a maximum of 24 hours.'time'Is the amount of time to wait. time can be specified in one of the acceptable formats for datetime data, or it can be specified as a local variable. Dates cannot be specified; therefore, the date portion of the datetime value is not allowed.TIMEInstructs SQL Server to wait until the specified time. RemarksAfter executing the WAITFOR statement, you cannot use your connection to SQL Server until the time or event that you specified occurs.To see the active and waiting processes, use sp_who.ExamplesA. Use WAITFOR TIME This example executes the stored procedure update_all_stats at 10:20 P.M. BEGIN WAITFOR TIME '22:20' EXECUTE update_all_statsENDFor more information about using this procedure to update all statistics for a database, see the examples in UPDATE STATISTICS. B. Use WAITFOR DELAY This example shows how a local variable can be used with the WAITFOR DELAY option. A stored procedure is created to wait for a variable amount of time and then returns information to the user as to the number of hours, minutes, and seconds that have elapsed.CREATE PROCEDURE time_delay @@DELAYLENGTH char(9)ASDECLARE @@RETURNINFO varchar(255)BEGIN WAITFOR DELAY @@DELAYLENGTH SELECT @@RETURNINFO = 'A total time of ' + SUBSTRING(@@DELAYLENGTH, 1, 3) + ' hours, ' + SUBSTRING(@@DELAYLENGTH, 5, 2) + ' minutes, and ' + SUBSTRING(@@DELAYLENGTH, 8, 2) + ' seconds, ' + 'has elapsed! Your time is up.' PRINT @@RETURNINFOENDGO-- This next statement executes the time_delay procedure.EXEC time_delay '000:00:10'GOHere is the result set:A total time of 000 hours, 00 minutes, and 10 seconds, has elapsed! Your time is up.©1988-2000 Microsoft Corporation. All Rights Reserved.
Corey |
 |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2004-08-27 : 11:35:42
|
| Dynamic SQL... roughly:set @sqlstr = 'Update myTable Set col01 = ''blah'''Exec(sqlStr)Corey |
 |
|
|
elwoos
Master Smack Fu Yak Hacker
2052 Posts |
Posted - 2004-09-01 : 05:54:44
|
| Many thankssteveSteve no function beer well without |
 |
|
|
|
|
|
|
|