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.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Dynamic SQL Issues

Author  Topic 

NaseemAfzal
Starting Member

2 Posts

Posted - 2014-05-20 : 18:13:01
Am new to SQL server , I have created the below procedure to create a table dynamically
ALTER PROC [dbo].[createLogTrigger]
(
@TableName VARCHAR(100)
)
AS
DECLARE
@SQLTRIG VARCHAR(4000),
@SQLLOGT nVARCHAR(4000);
BEGIN


SELECT SQLLOGT= ' CREATE TABLE ' +@TableName + '_AUDIT (
Audit_id [int] IDENTITY(1,1) NOT NULL, '
+ @TableName+'_id [int] NOT NULL ,
change_type varchar(10) NOT NULL,
Column_name varchar(100) ,
old_value varchar(400),
new_value varchar(400),
UserName varchar(20) NOT NULL,
UpdateDate varchar(21) NOT NULL)'

PRINT @SQLLOGT

EXEC sp_executesql @SQLLOGT

END

The procedure executes fine but table is not getting created. Please help me solving this

Thanks,

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-20 : 19:42:52
Show us the PRINT output.

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-20 : 19:43:23
Oh I see the issue: SELECT SQLLOGT ==> SELECT @SQLLOGT

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

NaseemAfzal
Starting Member

2 Posts

Posted - 2014-05-21 : 11:09:40
Thanks Tara Kizer!

Its my bad, I didn't notice.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2014-05-21 : 12:21:04


Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page
   

- Advertisement -