在SQL查询中使用'goto'语句是一种好习惯吗?
答案 0 :(得分:9)
取决于SQL - 某些方言不提供除GOTO之外的流控制的有用机制。
GOTO通常是不好的形式。
答案 1 :(得分:6)
不在生产代码中,但测试可能没问题。
例如,希望为存储过程提供回归测试,其中“公共位”是对正在测试的过程和调试语句的调用。
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 noob,我希望程序或函数在范围内声明做“公共位”,但这是我在google搜索之后想出的最好的。为什么要为要重用的每一段代码设置存储过程。特别是对于非生产工作。
答案 2 :(得分:3)
没有
与其他语言一样,使用Goto几乎总是有更好的选择。
如果你告诉我们你正在使用哪个SQL包以及你想要完成什么,我们或许可以让你知道哪个可能适合。
答案 3 :(得分:0)
我的猜测是否定的。我使用任何现代语言的goto语句的一般规则是,如果你使用它们,你的设计就会出现问题。
答案 4 :(得分:0)
转到是具有自己功能的关键字。 只要需要直接跳到某个级别,我们就可以使用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