I'm trying to create a user specific table. The table is used in a process to hold data before carrying out further calcualtions. Because multiple users might be running the process the table needs to be user specific.
I started off with a stored procedure that created a permanent table, but the thought it would be better to use a temporary table so that the table is dropped automatically once a user session is over.
Because the table is going to be referenced by other stored procedures I want to make it global. The code below works fine until I add the ## to the table name, then I get an error;
"Server: Msg 2714, Level 16, State 6, Line 1
There is already an object named '##tblCouponDetail_Admin' in the database."
I've put in a drop table statement before the create table, which works when I'm doing this with a table called 'tblCouponDetail_Admin'. Here's my code.
USE Reporter
IF EXISTS (SELECT name FROM sysobjects
WHERE name ='USP_CreateUserReportTable')
DROP PROCEDURE USP_CreateUserReportTable
GO
CREATE PROCEDURE USP_CreateUserReportTable (@UserID varchar(20))
AS
DECLARE @TableName nvarchar(30)
DECLARE @strSQL1 NVARCHAR(2000)
DECLARE @strSQL2 NVARCHAR(2000)
DECLARE @strSQL3 NVARCHAR(2000)
DECLARE @LongTableName nvarchar(38)
SET @TableName = '##tblCouponDetail_'+ @UserID
SET @LongTableName = '[dbo].[' + @TableName + ']'
SET QUOTED_IDENTIFIER ON
set @strSQL1 = "if exists (select * from sysobjects where id = object_id(N'" + @LongTableName + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table" + @LongTableName
SET @strSQL2 = 'CREATE TABLE ' + @TableName + ' (
[ContractNumber] [int] NULL ,
[NCHCode] [char] (10) NULL ,
[CouponBarcode] [char] (12) NULL ,
[Description] [nvarchar] (50) NULL ,
[Segment] [nvarchar] (50) NULL ,
[CouponType] [nvarchar] (50) NULL ,
[PrintingMCLU] [int] NOT NULL ,
[CouponValue] [money] NULL ,
[RequiredSpend] [money] NULL ,
[RedemptionMCLU] [int] NULL ,
[EstimatedRedemption] [real] NULL ,
[PromotionPeriod] [nvarchar] (50) NULL)
ON [PRIMARY]'
set @strSQL3 = 'ALTER TABLE [dbo].[tblCouponDetail_' + @UserID + '] WITH NOCHECK ADD
CONSTRAINT [PK_tblCouponDetail_' + @UserID + '] PRIMARY KEY NONCLUSTERED
(
[PrintingMCLU]
) ON [PRIMARY]'
EXECUTE(@strSQL1)
EXECUTE(@strSQL2)
EXECUTE(@strSQL3)
SET QUOTED_IDENTIFIER OFF
I started off with a stored procedure that created a permanent table, but the thought it would be better to use a temporary table so that the table is dropped automatically once a user session is over.
Because the table is going to be referenced by other stored procedures I want to make it global. The code below works fine until I add the ## to the table name, then I get an error;
"Server: Msg 2714, Level 16, State 6, Line 1
There is already an object named '##tblCouponDetail_Admin' in the database."
I've put in a drop table statement before the create table, which works when I'm doing this with a table called 'tblCouponDetail_Admin'. Here's my code.
USE Reporter
IF EXISTS (SELECT name FROM sysobjects
WHERE name ='USP_CreateUserReportTable')
DROP PROCEDURE USP_CreateUserReportTable
GO
CREATE PROCEDURE USP_CreateUserReportTable (@UserID varchar(20))
AS
DECLARE @TableName nvarchar(30)
DECLARE @strSQL1 NVARCHAR(2000)
DECLARE @strSQL2 NVARCHAR(2000)
DECLARE @strSQL3 NVARCHAR(2000)
DECLARE @LongTableName nvarchar(38)
SET @TableName = '##tblCouponDetail_'+ @UserID
SET @LongTableName = '[dbo].[' + @TableName + ']'
SET QUOTED_IDENTIFIER ON
set @strSQL1 = "if exists (select * from sysobjects where id = object_id(N'" + @LongTableName + "') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table" + @LongTableName
SET @strSQL2 = 'CREATE TABLE ' + @TableName + ' (
[ContractNumber] [int] NULL ,
[NCHCode] [char] (10) NULL ,
[CouponBarcode] [char] (12) NULL ,
[Description] [nvarchar] (50) NULL ,
[Segment] [nvarchar] (50) NULL ,
[CouponType] [nvarchar] (50) NULL ,
[PrintingMCLU] [int] NOT NULL ,
[CouponValue] [money] NULL ,
[RequiredSpend] [money] NULL ,
[RedemptionMCLU] [int] NULL ,
[EstimatedRedemption] [real] NULL ,
[PromotionPeriod] [nvarchar] (50) NULL)
ON [PRIMARY]'
set @strSQL3 = 'ALTER TABLE [dbo].[tblCouponDetail_' + @UserID + '] WITH NOCHECK ADD
CONSTRAINT [PK_tblCouponDetail_' + @UserID + '] PRIMARY KEY NONCLUSTERED
(
[PrintingMCLU]
) ON [PRIMARY]'
EXECUTE(@strSQL1)
EXECUTE(@strSQL2)
EXECUTE(@strSQL3)
SET QUOTED_IDENTIFIER OFF