Create dynamic sql from user name and date : Dynamic SQL « Transact SQL « SQL Server / T-SQL






Create dynamic sql from user name and date

 

3>
4> DECLARE @sqlstring varchar(100)
5> SELECT @sqlstring = 'SELECT * FROM ' + LTRIM(user_name()) +
6>                     CONVERT(char(2),DATEPART(dd, GETDATE()))
7> EXEC (@sqlstring)
8> GO
Msg 208, Level 16, State 1, Server J\SQLEXPRESS, Line 1
Invalid object name 'dbo17'.

 








Related examples in the same category

1.Use aggregate function in dynamic sql statement
2.Using a dynamic sql statement in 'Insert...select'
3.Build and call dynamic statement