Sunday, February 10, 2019
SQL Server - Truncate/Clear table variable in Sql Server
declare @TableCount TABLE (a INT);
To delete contents of above table variable
use the following command
DELETE FROM
@TableCount
Saturday, February 9, 2019
SQL Server - For/While Loop example
T-SQL doesn't have a
FOR
loop, it has a WHILE
loopWHILE Boolean_expression
BEGIN
END
USE <YourDatabaseName>
GO
DECLARE @inc INT; SET @inc=1; WHILE (@inc<=10)
BEGIN
print 'inside a while loop' SET @inc = @inc +1
END; PRINT 'Done WHILE LOOP'; GO
------------Another Example
USE MyDatabase
GO
DECLARE @inc INT;
SET @inc=100;
DECLARE @date1 Date;
SET @date1=GETDATE();
WHILE (@inc>=1)
BEGIN
SET @date1= DATEADD(DAY,-@inc, GETDATE());
print 'inside a while loop';
print @date1;
WITH TEMP as
( SELECT *
FROM Table1
WHERE ID IS NULL
AND dateupdated=@date1),
temp2 AS
(SELECT *
FROM Table2
WHERE 1=1
AND date2=@date1 )
SELECT *
FROM TEMP a
INNER JOIN table2 e
ON e.ID=a.ID
EXCEPT
SELECT *
FROM temp2 a
INNER JOIN table4 e
ON e.ID=a.ID;
SET @inc = @inc - 1;
END;
PRINT 'Done WHILE LOOP';
GO
Subscribe to:
Posts (Atom)
Recently Executed queries
SELECT txt.TEXT AS [SQL Statement], qs.EXECUTION_COUNT [No. Times Executed], qs.LAST_EXECUTION_TIME AS [Last Time Executed], ...
-
Msg 155, Level 15, State 2, Line 1 'datetime' is not a recognized CURSOR option. We get an error of this kind when we miss...
-
Msg 1033, Level 15, State 1, Procedure xxxx , Line 105 [Batch Start Line 0] The ORDER BY clause is invalid in views, inline functions, deri...
-
This is an error that is coming from SQL server and not from windows batch script. If you are passing Parameter with special characters ...