May 30, 2006 at 7:23 am
Hi, everybody
I want to create a table name but the table's name must use a parameter
for example
declare @ca varchar(20)
declare @fechai datetime
declare @personaltablename varchar (256)
set @fechai = dateadd(mm,-1,getdate())
set @ca = convert(varchar, @fechai, 111)
set @ca = substring(@ca,1,8) + '01'
set @personaltablename ='transof' + cast(year(@ca) as varchar) + cast(month(@ca) as varchar)
CREATE TABLE @personaltablename (
[field1] [int] IDENTITY (1, 1) NOT NULL ,
[field2] [int] NOT NULL DEFAULT (0),
[field3] [int] NOT NULL
)
GO
what's wrong and how can i generate corectly a table name
Note: I push the variable @personaltablename in brackets []
Thanks in advance.
May 30, 2006 at 7:28 am
You need to build the create table statement in dynamic SQL
declare @ca varchar(20)
declare @fechai datetime
declare @personaltablename varchar (256)
declare @sql varchar(5000)
set @fechai = dateadd(mm,-1,getdate())
set @ca = convert(varchar, @fechai, 111)
set @ca = substring(@ca,1,8) + '01'
set @personaltablename ='transof' + cast(year(@ca) as varchar) + cast(month(@ca) as varchar)
set @sql =
'CREATE TABLE '+@personaltablename+' (
[field1] [int] IDENTITY (1, 1) NOT NULL ,
[field2] [int] NOT NULL DEFAULT (0),
[field3] [int] NOT NULL
)'
exec (@sql)
May 30, 2006 at 10:30 am
Or
nocount on set ansi_warnings off
table dbo.temptablename_forproc1432
table dbo.temptablename_forproc1432( field1 int identity not null,
@fechai datetime, @personaltablename sysname
sp_rename N'dbo.temptablename_forproc1432', @personaltablename
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 30, 2006 at 4:43 pm
Couple of years ago one guy asked the author of the same king of question about he name of conpany he's working for. To aviod using products of that company ander any circumstanses.
Seems we've got flood of not bad, but terrible approaches made by people not qualified to type even one row in T-SQL.
Microsoft sentenced SQL Server to death by issuing SQL Server 2005.
This thing invites developer into database development, makes them think there is no much difference, just another syntax.
In a year or two almost every system built on SQL Server gonna be slow, non scalable, non manageble, not reliable. It will become next MS Access - home application database.
MS Access is not acceptable for commercial application, so MS SQL Server goes the same way.
Not because of the quality of the product but because of the quality of programmers allowed to develop on this platform.
_____________
Code for TallyGenerator
May 30, 2006 at 4:56 pm
What is the revlevance of your post, Sergei?
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 31, 2006 at 7:51 am
Sorry I misspelt your name Sergiy. Still I'm afraid your dark mutterings about procedural programmers (not me) don't cut any ice unless backed with some sort of substantive criticism.
Tim Wilkinson
"If it doesn't work in practice, you're using the wrong theory"
- Immanuel Kant
May 31, 2006 at 8:12 am
There is nothing inherently procedural about issuing dynamic ddl commands.
Having generic, automated methods of building relational structures should only increase the relational solidity of sql dbs by cutting out human error and instantiating a 'best practise' standard within it.
All the code in this thread is sql 2000 compatible.
Relying on sys sps like sp_rename is arguably more flaky than using ansi standard ddl
...
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply