北京網(wǎng)站設(shè)計(jì) 公司丁丁貓:今天有一老同學(xué)問(wèn)我一個(gè)用存儲(chǔ)過(guò)程創(chuàng)建數(shù)據(jù)表的問(wèn)題,這可把我問(wèn)住了,我不會(huì)用SQLServer的存儲(chǔ)過(guò)程創(chuàng)建數(shù)據(jù)表!就是ORACLE我也剛剛學(xué)會(huì)如何用動(dòng)態(tài)SQL創(chuàng)建數(shù)據(jù)表!我對(duì)SQLServer的存儲(chǔ)過(guò)程本來(lái)就是一無(wú)所知啊! 請(qǐng)教了我的同事,也是不知道如何弄!暈,看我們這些程序員當(dāng)?shù)模? 問(wèn)了一前的一個(gè)客戶,到現(xiàn)在還沒(méi)有答復(fù).哎! 只好硬著頭皮搞了! 以下是錯(cuò)誤的: CREATE PROCEDURE [dbo].[CTable] @TABLE_NAME VARCHAR AS CREATE TABLE [dbo].[@TABLE_NAME] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [Ww_Name] [varchar] (200) COLLATE Chinese_PRC_CI_AS NOT NULL , [Ww_Category] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [Ww_Sum] [int] NULL , [Ww_Price] [money] NULL , [Ww_SumPrice] [money] NULL , [Ww_Operator] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Ww_Jren] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Ww_Jdepartment] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [Ww_JMemo] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL , [Ww_OITime] [datetime] NULL , [Ww_Memo] [text] COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO EXEC CTable test 用上面的建的數(shù)據(jù)表名字就是 @TABLE_NAME,太錯(cuò)了! 以下是正確的過(guò)程 CREATE PROCEDURE [dbo].[CTable] @TABLE_NAME VARCHAR(4) AS BEGIN EXEC( CREATE TABLE [dbo].[+@TABLE_NAME+] ( [ID] [int] IDENTITY (1, 1) NOT NULL , [Ww_Name] [varchar] (200) COLLATE Chinese_PRC_CI_AS NOT NULL , [Ww_Category] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [Ww_Sum] [int] NULL , [Ww_Price] [money] NULL , [Ww_SumPrice] [money] NULL , [Ww_Operator] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Ww_Jren] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL , [Ww_Jdepartment] [varchar] (100) COLLATE Chinese_PRC_CI_AS NULL , [Ww_JMemo] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL , [Ww_OITime] [datetime] NULL , [Ww_Memo] [text] COLLATE Chinese_PRC_CI_AS NULL ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] ) END GO 原來(lái)也是用字符串拼的??!就像Oracle里的一樣(類似于ORACLE里的動(dòng)態(tài)SQL)
轉(zhuǎn)發(fā)請(qǐng)注明:丁丁貓