以编程方式为表生成脚本

问题描述 投票:0回答:2

我学习了如何为表生成脚本。

例如这张表:

生成这样的脚本(我省略了一些内容):

CREATE TABLE [dbo].[singer_and_album](
    [singer] [varchar](50) NULL,
    [album_title] [varchar](100) NULL
) ON [PRIMARY]
GO

INSERT [dbo].[test_double_quote] ([singer], [album_title]) VALUES (N'Adale', N'19')
GO

INSERT [dbo].[test_double_quote] ([singer], [album_title]) VALUES (N'Michael Jaskson', N'Thriller"')
GO

我尝试使用“这个 shell 代码”以编程方式生成脚本。并出现错误:

PS SQLSERVER:\SQL\DESKTOP-KHTRJOJ\MSSQL\Databases\yzhang\Tables\dbo.test_double_quote> C:\Users\yzhang\Documents\script_out_table.ps1“DESKTOP-KHTRJOJ\MSSQL” “yzhang”“dbo”“test_double_quote”, “C:\Users\yzhang\Documents\script_out.sql”

“EnumScript”和参数发现多个不明确的重载 计数:“1”。在 C:\Users\yzhang\Documents\script_out_table.ps1:41 字符数:16 + foreach ($s in $scripter.EnumScript($tbl.Urn)) { 写入主机 $s } + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 类别信息:未指定:(:) [],MethodException +FullyQualifiedErrorId:MethodCountCouldNotFindBest

有人可以帮忙吗?我对shell了解不多。 顺便说一句,shell 是生成脚本的唯一方法吗?我们可以用一些sql代码来完成吗?谢谢你--

仅供参考,请参阅
this

了解如何手动生成脚本。就我而言(sql server 2016 management studio)就像

右键数据库名称(不是表名称)->
    Tasks
  1. ->
    Generate scripts
    
    
    

    选择一张桌子或所有桌子
  1. 单击
  2. advanced

    并选择

    schema and data
    
    

sql-server copy
2个回答
5
投票

declare @vsSQL varchar(8000) declare @vsTableName varchar(50) select @vsTableName = '_PRODUCT'--- Your Table Name here select @vsSQL = 'CREATE TABLE ' + @vsTableName + char(10) + '(' + char(10) select @vsSQL = @vsSQL + ' ' + sc.Name + ' ' + st.Name + case when st.Name in ('varchar','varchar','char','nchar') then '(' + cast(sc.Length as varchar) + ') ' else ' ' end + case when sc.IsNullable = 1 then 'NULL' else 'NOT NULL' end + ',' + char(10) from sysobjects so join syscolumns sc on sc.id = so.id join systypes st on st.xusertype = sc.xusertype where so.name = @vsTableName order by sc.ColID select substring(@vsSQL,1,len(@vsSQL) - 2) + char(10) + ')'

编辑:

c#代码 public string GetScript(string strConnectionString , string strObject , int ObjType) { string strScript = null; int intCounter = 0; if (ObjType != 0) { ObjSqlConnection = new SqlConnection(strConnectionString.Trim()); try { ObjDataSet = new DataSet(); ObjSqlCommand = new SqlCommand("exec sp_helptext [" + strObject + "]", ObjSqlConnection); ObjSqlDataAdapter = new SqlDataAdapter(); ObjSqlDataAdapter.SelectCommand = ObjSqlCommand; ObjSqlDataAdapter.Fill(ObjDataSet); foreach (DataRow ObjDataRow in ObjDataSet.Tables[0].Rows) { strScript += Convert.ToString(ObjDataSet.Tables[0].Rows[intCounter][0]); intCounter++; } } catch (Exception ex) { strScript = ex.Message.ToString(); } finally { ObjSqlDataAdapter = null; ObjSqlCommand = null; ObjSqlConnection = null; } } return strScript; }

要创建插入脚本,请使用此存储过程 

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[InsertGenerator]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1) DROP PROCEDURE [dbo].[InsertGenerator] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER OFF GO CREATE PROC [dbo].[InsertGenerator] ( @tableName varchar(100), @KeyColumn1 varchar(100)='', @KeyColumn2 varchar(100)='' ) AS -- Generating INSERT statements in SQL Server -- to validate if record exists - supports 2 field Unique index --Declare a cursor to retrieve column specific information for the specified table DECLARE cursCol CURSOR FAST_FORWARD FOR SELECT column_name,data_type FROM information_schema.columns WHERE table_name = @tableName OPEN cursCol DECLARE @string nvarchar(max) --for storing the first half of INSERT statement DECLARE @stringData nvarchar(max) --for storing the data (VALUES) related statement DECLARE @dataType nvarchar(1000) --data types returned for respective columns DECLARE @FieldVal nvarchar(1000) -- save value for the current field DECLARE @KeyVal nvarchar(1000) -- save value for the current field DECLARE @KeyTest0 nvarchar(1000) -- used to test if key exists DECLARE @KeyTest1 nvarchar(1000) -- used to test if key exists DECLARE @KeyTest2 nvarchar(1000) -- used to test if key exists SET @KeyTest0='' IF @KeyColumn1<>'' SET @KeyTest0='IF not exists (Select * from '+@tableName SET @KeyTest1='' SET @KeyTest2='' SET @string='INSERT '+@tableName+'(' SET @stringData='' SET @FieldVal='' SET @KeyVal='' DECLARE @colName nvarchar(50) FETCH NEXT FROM cursCol INTO @colName,@dataType IF @@fetch_status<>0 begin print 'Table '+@tableName+' not found, processing skipped.' close curscol deallocate curscol return END WHILE @@FETCH_STATUS=0 BEGIN IF @dataType in ('varchar','char','nchar','nvarchar') BEGIN SET @FieldVal=''''+'''+isnull('''''+'''''+'+@colName+'+'''''+''''',''NULL'')+'',''+' SET @KeyVal='''+isnull('''''+'''''+'+@colName+'+'''''+''''',''NULL'')+'',''+' SET @stringData=@stringData+@FieldVal END ELSE if @dataType in ('text','ntext','xml') --if the datatype is text or something else BEGIN SET @FieldVal='''''''''+isnull(cast('+@colName+' as varchar(max)),'''')+'''''',''+' SET @stringData=@stringData+@FieldVal END ELSE IF @dataType = 'money' --because money doesn't get converted from varchar implicitly BEGIN SET @FieldVal='''convert(money,''''''+isnull(cast('+@colName+' as varchar(200)),''0.0000'')+''''''),''+' SET @stringData=@stringData+@FieldVal END ELSE IF @dataType='datetime' BEGIN SET @FieldVal='''convert(datetime,'+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+',121)+'''''+''''',''NULL'')+'',121),''+' SET @stringData=@stringData+@FieldVal END ELSE IF @dataType='image' BEGIN SET @FieldVal='''''''''+isnull(cast(convert(varbinary,'+@colName+') as varchar(6)),''0'')+'''''',''+' SET @stringData=@stringData+@FieldVal END ELSE --presuming the data type is int,bit,numeric,decimal BEGIN SET @FieldVal=''''+'''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+' SET @KeyVal='''+isnull('''''+'''''+convert(varchar(200),'+@colName+')+'''''+''''',''NULL'')+'',''+' SET @stringData=@stringData+@FieldVal END --Build key test IF @KeyColumn1=@colName begin SET @KeyTest1 = ' WHERE [' + @KeyColumn1 + ']=' SET @KeyTest1 = @KeyTest1+@KeyVal+']' end IF @KeyColumn2=@colName begin SET @KeyTest2 = ' AND [' + @KeyColumn2 + ']=' SET @KeyTest2 = @KeyTest2+@KeyVal+']' end SET @string=@string+'['+@colName+'],' FETCH NEXT FROM cursCol INTO @colName,@dataType END DECLARE @Query nvarchar(max) -- Build the test string to check if record exists if @KeyTest0<>'' begin if @Keycolumn1<>'' SET @KeyTest0 = @KeyTest0 + substring(@KeyTest1,0,len(@KeyTest1)-4) if @Keycolumn2<>'' begin SET @KeyTest0 = @KeyTest0 + '''' SET @KeyTest0 = @KeyTest0 + substring(@KeyTest2,0,len(@KeyTest2)-4) end SET @KeyTest0 = @KeyTest0 + ''')' SET @query ='SELECT '''+substring(@KeyTest0,0,len(@KeyTest0)) + ') ' end else SET @query ='SELECT '''+substring(@KeyTest0,0,len(@KeyTest0)) SET @query = @query + substring(@string,0,len(@string)) + ') ' SET @query = @query + 'VALUES(''+ ' + substring(@stringData,0,len(@stringData)-2)+'''+'')'' FROM '+@tableName exec sp_executesql @query CLOSE cursCol DEALLOCATE cursCol GO

以及 InsertGenerator 的使用,如下所示 

DECLARE @return_value int EXEC @return_value = [dbo].[InsertGenerator] @tableName = N'_PRODUCT' SELECT 'Return Value' = @return_value



0
投票

格式也与 SSMS 输出类似。

declare @crlf nchar(2); set @crlf = char(13) + char(10); declare @createScript nvarchar(max) declare @tableName nvarchar(128) select @tableName = N'singer_and_album'--- Your Table Name here select @createScript = N'CREATE TABLE [dbo].[' + @tableName + N'](' + @crlf select @createScript = @createScript + char(9) + N'[' + sc.[name] + N'] ' + case when sc.is_computed = 1 then N' AS ' + cc.[definition] else st.[name] + case when st.[name] in ('varchar','nvarchar','char','nchar') then '(' + case when sc.max_length = -1 then N'max' else cast(sc.max_length as varchar) end + N')' when st.[name] in ('decimal','numeric') then N'(' + cast(sc.precision as varchar) + N',' + cast(sc.scale as varchar) + N')' else N'' end + case when sc.is_identity = 1 then N' IDENTITY(1,1) ' else N' ' end + case when sc.is_nullable = 1 then N'NULL' else N'NOT NULL' end end + N',' + @crlf from sys.columns sc join sys.types st on st.user_type_id = sc.user_type_id left outer join sys.computed_columns cc on cc.[object_id] = sc.[object_id] and cc.column_id = sc.[column_id] where sc.[object_id] = object_id(@tableName) order by sc.column_id; set @createScript = left(@createScript,len(@createScript) - 3); -- trim last comma if exists(select * from sys.indexes where [object_id] = object_id(@tableName) and is_primary_key = 1) begin set @createScript = @createScript + N',' + @crlf + N' CONSTRAINT [' + (select top 1 [name] from sys.indexes where object_id = object_id(@tableName) and is_primary_key = 1) + N'] PRIMARY KEY CLUSTERED ' + @crlf + N'(' + @crlf; select @createScript = @createScript + char(9) + N'[' + sc.[name] + N'] ' + case when ic.is_descending_key = 1 then N'DESC' else N'ASC' end + N',' + @crlf from sys.index_columns ic join sys.columns sc on ic.[object_id] = sc.[object_id] and ic.column_id = sc.column_id join sys.indexes si on ic.[object_id] = si.[object_id] and ic.index_id = si.index_id where ic.object_id = object_id(@tableName) and si.is_primary_key = 1 order by ic.index_column_id; set @createScript = left(@createScript,len(@createScript) - 3) + @crlf + N')' -- trim last comma end select @createScript + @crlf + N')';

© www.soinside.com 2019 - 2024. All rights reserved.