Monday, June 18, 2012

How to create and populate a temporary table in SQL.

USE [Foo]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE Whatever
AS
BEGIN
   CREATE TABLE #squarednumbers
   (
      numericvalue INT
   )
   declare @unalterednumber INT
   DECLARE curs CURSOR FOR SELECT TOP 10 Bar From dbo.[Baz]
   OPEN curs
   FETCH NEXT FROM curs into @unalterednumber
   WHILE @@FETCH_STATUS = 0
      BEGIN
         INSERT INTO #squarednumbers (numericvalue)
         VALUES (@unalterednumber * @unalterednumber)
         FETCH NEXT FROM curs into @unalterednumber
      END
   CLOSE curs
   DEALLOCATE curs
   SELECT * FROM #squarednumbers
END

No comments:

Post a Comment