我想知道是否有解决办法在许多数据库中创建一个新表取决于他们的名字。
我正在使用SQL Server 2008 R2。
这是我创建新表的脚本:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[E_Invent2](
[D_No] [smallint] NOT NULL,
[Mo_Droits] [nvarchar](1) NOT NULL,
[Pr_Code] [nvarchar](10) NOT NULL,
[Pr_Libellé] [nvarchar](50) NULL,
[In_Stk08] [real] NULL,
[In_Stk09] [real] NULL,
[In_Stk10] [real] NULL,
[In_Stk11] [real] NULL,
[In_Stk12] [real] NULL,
[In_Stk01] [real] NULL,
[In_Stk02] [real] NULL,
[In_Stk03] [real] NULL,
[In_Stk04] [real] NULL,
[In_Stk05] [real] NULL,
[In_Stk06] [real] NULL,
[In_Stk07] [real] NULL,
[In_StkMoy] [real] NULL,
[In_StkReel] [real] NULL,
[In_PrtConstElab] [real] NULL,
[In_TauxElab] [real] NULL,
[In_DeducAutoElab] [real] NULL,
[In_PrtTaxElab] [real] NULL,
[In_PrtConstEmbout] [real] NULL,
[In_VolEmbout] [real] NULL,
[In_TauxEmbout] [real] NULL,
[In_DeducAutoEmbout] [real] NULL,
[In_PrtTaxEmbout] [real] NULL,
[In_PrtTaxTotal] [real] NULL,
[In_ManquantTaxable] [real] NULL,
[In_TotalDroits] [real] NULL,
[D_Nom] [nchar](50) NULL,
[D_Enseigne] [nchar](30) NULL,
[D_Adr1] [nchar](30) NULL,
[D_Adr2] [nchar](30) NULL,
[D_Post] [nchar](10) NULL,
[D_Adr3] [nchar](30) NULL,
CONSTRAINT [PK_E_Invent2] PRIMARY KEY CLUSTERED
(
[D_No] ASC,
[Mo_Droits] ASC,
[Pr_Code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
我想为数据库的名称以CM_0开头的所有数据库执行它。
我找到了丢弃表的解决方案:
declare @sql varchar(8000)
set @sql=''
select @sql=@sql+' drop table '+table_name from INFORMATION_SCEHMA.TABLES where table_name like 'tmp_%'
exec(@sql)
但是'掉落'命令行比创建'更简单。命令行......
有人可以帮助我吗?
提前致谢
Cooxkie
答案 0 :(得分:2)
有一种更好,更便宜的方法。这非常简单,效果很好。
使用SELECT INTO
语句,您可以将表格结构和数据复制到相同或外部数据库中的另一个表格。
参考:http://www.w3schools.com/sql/sql_select_into.asp
DECLARE @sql VARCHAR(8000)
SET @sql=''
SELECT @sql=@sql+'; SELECT * INTO '+name+'.dbo.E_Invent2 FROM OriginalDB.dbo.E_Invent2' FROM sysdatabases WHERE name LIKE 'CM_0%' and name<>'OriginalDB'
SELECT @sql
EXEC(@sql)
此处OrigialDB
是您拥有此表格的数据库的名称
如果OrginalDB
中的表格包含数据并且您不想复制数据并且只需要复制结构,那么您可以试试这个 -
DECLARE @sql VARCHAR(8000)
SET @sql=''
SELECT @sql=@sql+'; SELECT * INTO '+name+'.dbo.E_Invent2 FROM OriginalDB.dbo.E_Invent2 WHERE 1<>1' FROM sysdatabases WHERE name LIKE 'CM_0%' and name<>'OriginalDB'
SELECT @sql
EXEC(@sql)
如果能帮助你,请告诉我。
注意:不会复制约束
答案 1 :(得分:0)
请检查:
Declare @Sql nvarchar(max) = ''
Declare @tablename nvarchar(max) = 'MyTab'
Declare @PreSyntax nvarchar(max) = 'SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE '
Declare @PostSyntax nvarchar(max) = '(
[D_No] [smallint] NOT NULL,
[Mo_Droits] [nvarchar](1) NOT NULL,
[Pr_Code] [nvarchar](10) NOT NULL,
[Pr_Libellé] [nvarchar](50) NULL,
[In_Stk08] [real] NULL,
[In_Stk09] [real] NULL,
[In_Stk10] [real] NULL,
[In_Stk11] [real] NULL,
[In_Stk12] [real] NULL,
[In_Stk01] [real] NULL,
[In_Stk02] [real] NULL,
[In_Stk03] [real] NULL,
[In_Stk04] [real] NULL,
[In_Stk05] [real] NULL,
[In_Stk06] [real] NULL,
[In_Stk07] [real] NULL,
[In_StkMoy] [real] NULL,
[In_StkReel] [real] NULL,
[In_PrtConstElab] [real] NULL,
[In_TauxElab] [real] NULL,
[In_DeducAutoElab] [real] NULL,
[In_PrtTaxElab] [real] NULL,
[In_PrtConstEmbout] [real] NULL,
[In_VolEmbout] [real] NULL,
[In_TauxEmbout] [real] NULL,
[In_DeducAutoEmbout] [real] NULL,
[In_PrtTaxEmbout] [real] NULL,
[In_PrtTaxTotal] [real] NULL,
[In_ManquantTaxable] [real] NULL,
[In_TotalDroits] [real] NULL,
[D_Nom] [nchar](50) NULL,
[D_Enseigne] [nchar](30) NULL,
[D_Adr1] [nchar](30) NULL,
[D_Adr2] [nchar](30) NULL,
[D_Post] [nchar](10) NULL,
[D_Adr3] [nchar](30) NULL,
CONSTRAINT [PK_E_Invent2] PRIMARY KEY CLUSTERED
(
[D_No] ASC,
[Mo_Droits] ASC,
[Pr_Code] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]'
Select @Sql = @Sql + char(10) + @PreSyntax + isnull(name,'') + '.dbo.' + @tablename + @PostSyntax + char(10)
from sys.sysdatabases d
where d.name like '%pattern%'
Select @Sql