Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2001-05-28 : 09:42:09
|
Michael writes "I am trying to pass in a MMYY date (e.g. 1201) as a parameter when I create a stored procedure, use that date to build a table name, and create and access the new table. However, I am getting an error when trying to use the variable that holds the table name (for example 'Incorrect syntax near @DateTable' is generated from the line DROP TABLE @DateTable).
Does anyone know how to reference a table using a variable? SQL Code:
IF EXISTS (SELECT name FROM sysobjects WHERE name = 'TestDateInput' AND type = 'P') DROP PROCEDURE TestDateInput GO
-- Create a new procedure CREATE PROCEDURE TestDateInput @DateIn char(4) AS
DECLARE @DateTable char(12) SET @DateTable = 'TestDate'+@DateIn
-- Create a table using DateTable variable IF EXISTS (SELECT name FROM sysobjects WHERE name = @DateTable AND type = 'U') DROP TABLE @DateTable
-- Create a new Date table CREATE TABLE @DateTable ( FIELDA char(3) NULL, FIELDB money DEFAULT 0 );
-- Insert a record INSERT @DateTable VALUES('AAA',100)
-- Do a SELECT SELECT * FROM @DateTable
GO" |
|
|
|
|
|