问题
Is it good practice to use 'goto' statements in SQL queries?
回答1:
Depends on the SQL - some of the dialects don't provide a useful mechanism for flow control other than GOTO.
GOTO is generally bad form.
回答2:
Not in production code but for testing could be ok.
For example, wanting to provide regression testing for a stored procedure where the "common bit" is the call to the procedure being tested and debug statements.
declare @test int;
set @test = 1;
goto tests
common:
print "common bit"
tests:
if @test = 1 print "1";
if @test = 2 print "2";
if @test = 3 print "3";
set @test = @test + 1;
if @test <= 3 goto common
print "finished " + cast(@test as varchar(5))
go -- goto can not be used past go!
Being a t-sql noob I was hoping for procedure or function to declare within scope to do the "common bit" but this was the best I could come up with after much googling. Why would you have to setup a stored procedure for every bit of code you want to re-use. Especially for non production work.
回答3:
No.
As with other languages, there's almost always a better option to use than a Goto.
If you tell us which SQL Package you're using and what you're trying to accomplish, we might be able to give you an idea as to exactly which might fit.
回答4:
My guess would be no. My general rule with goto statements in any modern language is, if your using them, something is wrong with your design.
回答5:
goto is keyword with its own feature. We can use goto whenever it is required to jump directly to some level.
Lets take the example... In my stored procedure I need to work with data taking in 4 temp tables. at every level after inserting records in temp table I need to check whether records are present in this temp temp or not, if no records got inserted so instead of processing further I can directly jump down using goto. label is the point where we should jump:
CREATE TABLE #tmpMsNos (custPo CHAR(24))
CREATE TABLE #tmpValidBilltos (billto CHAR(12))
CREATE TABLE #tmpOrders (
fh_pkey INT
,fh_id CHAR(8)
,custPo CHAR(24)
,lastchOfCustInsert DATETIME
)
CREATE TABLE #tmpOrdersFiltered (
fh_pkey INT
,fh_id CHAR(8)
,custPo CHAR(24)
,lastchOfCustInsert DATETIME
,onbDate DATETIME
,rapDate DATETIME
)
CREATE TABLE #tmpLoad (
custPo CHAR(24)
,ld_pkey INT
,ld_wkpmpn CHAR(25)
,lda_barcode VARCHAR(30)
,ld_createdOn DATETIME
,ReceivedDate DATETIME
,DispatchedDate DATETIME
)
INSERT INTO #tmpMsNos
SELECT cast(itemValue AS CHAR(24))
FROM dbo.fn_array_to_table(@pMsNos, ',')
IF (
NOT EXISTS (
SELECT 1
FROM #tmpMsNos
)
)
BEGIN
GOTO label
END
INSERT INTO #tmpValidBilltos
SELECT CONVERT(CHAR(12), xm_doref)
FROM xmlref x
WHERE xm_element = 'THD-BoxtruckRequest'
AND xm_attribute = 'THD-BoxtruckBillto'
IF (
NOT EXISTS (
SELECT 1
FROM #tmpValidBilltos
)
)
BEGIN
GOTO label
END
INSERT INTO #tmpOrders
SELECT fh.fh_pkey
,fh.fh_id
,fh.fh_custPo
,max(coc.ch_dt)
FROM #tmpMsNos msNos
INNER JOIN fcfgthd fh ON msNos.custPo = fh.fh_custPo
INNER JOIN #tmpValidBilltos bt ON bt.billto = fh.fh_bt_id
LEFT JOIN chofcust coc ON coc.ch_fhpkey = fh.fh_pkey
WHERE fh.fh_statcode NOT IN (
98 --CAN
,99 --DEL
)
AND fh.fh_ship_dt > @startDate
GROUP BY fh.fh_pkey
,fh.fh_id
,fh.fh_custPo
IF (
NOT EXISTS (
SELECT 1
FROM #tmpOrders
)
)
BEGIN
GOTO label
END
INSERT INTO #tmpOrdersFiltered
SELECT t.fh_pkey
,t.fh_id
,t.custPo
,t.lastchOfCustInsert
,MAX(cocONB.ch_dt)
,MAX(cocRAP.ch_dt)
FROM (
SELECT tmpO.fh_pkey
,tmpo.fh_id
,tmpO.custPo
,tmpO.lastchOfCustInsert
FROM #tmpOrders tmpO
INNER JOIN (
SELECT custpo
,max(lastchOfCustInsert) AS MaxInserteddate
FROM #tmpOrders
GROUP BY custpo
) tmpOgrouped ON tmpO.custpo = tmpOgrouped.custpo
AND tmpO.lastchOfCustInsert = tmpOgrouped.MaxInserteddate
) AS t
LEFT JOIN chofcust cocRAP ON cocRAP.ch_fhpkey = t.fh_pkey
AND cocRAP.ch_stat = 2 -- RAP --TODO: Add comment with status code like 98, 99 -- CAN, DEL for readability - Paresh
LEFT JOIN chofcust cocONB ON cocONB.ch_fhpkey = t.fh_pkey
AND cocONB.ch_stat = 5 -- ONB --TODO: Add comment with status code like 98, 99 -- CAN, DEL for readability - Paresh
GROUP BY t.fh_pkey
,t.fh_id
,t.custPo
,t.lastchOfCustInsert
--TODO: Take an exit if no order found into #tmpOrdersFiltered table, while taking a early exit make sure it doesn't break the calling code (C#) - Paresh
IF (
NOT EXISTS (
SELECT 1
FROM #tmpOrdersFiltered
)
)
BEGIN
GOTO label
END
INSERT INTO #tmpLoad
SELECT o.custPo
,l.ld_pkey
,l.ld_wkpmpn
,la.lda_barcode
,max(coc.ch_dt)
,CASE ISNULL(w.xl_date, '')
WHEN ''
THEN o.rapDate
ELSE w.xl_date
END AS ReceivedDate
,CASE ISNULL(mm.me_ecpkey, '')
WHEN ''
THEN o.ONBDate
ELSE NULL
END AS DispatchedDate
FROM #tmpOrdersFiltered o
INNER JOIN fcload l ON l.ld_fhpkey = o.fh_pkey
LEFT JOIN loadanc la ON la.lda_ldpkey = l.ld_pkey
LEFT JOIN wkxaclog w ON w.xl_ldpkey = l.ld_pkey
LEFT JOIN multiexceps mm ON mm.me_ldpkey = l.ld_pkey
AND mm.me_ecpkey = @missingitemexcep
LEFT JOIN chofcust COC ON coc.ch_ldpkey = l.ld_pkey
AND coc.ch_stat = 64 -- 64= ILH
GROUP BY o.custPo
,l.ld_pkey
,l.ld_wkpmpn
,la.lda_barcode
,w.xl_date
,o.rapDate
,mm.me_ecpkey
,o.ONBDate
来源:https://stackoverflow.com/questions/3046017/sql-goto-statement