新人求帮助!谢谢各位大神
本人初学SQL,很多疑惑,求大神解答
1、什么情况下需要使用变量呢?
2、SET NOCOUNT ON SETNOCOUNT OFF是一起用还是分开呢?
例子
USE master
GO
SET NOCOUNT ON
DECLARE @startdate datetime, @adddays int
SET @startdate = '1/10/1900 12:00 AM'
SET @adddays = 5
SET NOCOUNT OFF
SELECT @startdate + 1.25 AS 'Start Date',
@startdate + @adddays AS 'Add Date'
下面是结果集:
Start Date Add Date
--------------------------- ---------------------------
Jan 11 1900 6:00AM Jan 15 1900 12:00AM
(1 row(s) affected)
在这个例子中为什么用了 SET NOCOUNT ON之后,还是有返回记录数?
------解决方案--------------------USE master
GO
SET NOCOUNT ON
DECLARE @startdate datetime, @adddays int
SET @startdate = '1/10/1900 12:00 AM'
SET @adddays = 5
SET NOCOUNT OFF
SELECT @startdate + 1.25 AS 'Start Date',
@startdate + @adddays AS 'Add Date'
这个返回行数的原因是因为你提前关了NOCOUNT,而真正返回数据的是最后一个SELECT,前面的知识赋值而已。所以要么注释,要么把OFF移到最后
------解决方案--------------------这样就不会再有返回记录了:
USE master
GO
SET NOCOUNT ON
DECLARE @startdate datetime, @adddays int
SET @startdate = '1/10/1900 12:00 AM'
SET @adddays = 5
--SET NOCOUNT OFF
SELECT @startdate + 1.25 AS 'Start Date',
@startdate + @adddays AS 'Add Date'
/*
Start Date Add Date
----------------------- -----------------------
1900-01-11 06:00:00.000 1900-01-15 00:00:00.000
*/