在SQL查询中使用'goto'语句是一个好的实践吗?
根据具体的SQL方言而定 - 有些方言除了GOTO之外并没有提供有效的流程控制机制。
GOTO通常是不良的形式。
虽然不适用于生产代码,但在测试中可能是可以的。
例如,希望为一个存储过程提供回归测试,在测试过程中需要调用待测试的存储过程以及调试语句。
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!
作为一个 T-SQL 初学者,我希望有一种在范围内声明的过程或函数来完成“共同部分”,但是在经过许多搜索后,这是我能想到的最好办法。为什么您需要为每个想要重用的代码设置存储过程呢?特别是对于非生产工作来说。
不。
与其他语言一样,使用goto语句通常有更好的替代方案。
如果您告诉我们您正在使用哪个SQL包以及您想要实现什么目标,我们可能能够为您提供确切的建议。
我猜应该是不行的。在任何现代语言中,我的一般规则是,如果你使用goto语句,那么你的设计肯定有问题。
goto 是一个具有自己特点的关键字。 我们可以在需要直接跳转到某个级别时使用 goto。
让我们来看一个例子... 在我的存储过程中,我需要使用 4 个临时表处理数据。 在每个级别中,在插入记录到临时表后,我需要检查这个临时表中是否存在记录,如果没有插入记录,那么我可以直接使用 goto 跳转到下一个级别。 标签是我们应该跳转的地方:
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
GOTO
- 只有T-SQL。 - user330315