測試時需要部署空的資料庫,除了資料庫結構外,還需要將程式執行過程中所需要的基礎資料建立好,以下儲存過程,可一次性將基礎資料寫成insert語句,方便部署新庫,使用時,傳入表名即可。
這裡用到了系統表syscolumns中的xtype欄位,xtype具體含義如下:
34 image,35 text,36 uniqueidentifier,48 tinyint,52 smallint,56 int,58 smalldatetime,59 real,60 money,61 datetime,62 float,98 sql_variant,99 ntext,104 bit,106 decimal,108 numeric,122 smallmoney,127 bigint,165 varbinary,167 varchar,173 binary,175 char,189 timestamp,231 sysname,231 nvarchar,239 nchar。
利用這些數字識別將要插入欄位的型別。用一下儲存過程即可,注:該儲存過程也將主鍵匯出了。
-- description: 該儲存過程返回的是參數列中的資料
set ansi_nulls on
set quoted_identifier on
gocreate proc [dbo].[spgeninsertsql] (@tablename varchar(256))
asbegin
declare @sql varchar(8000)
declare @sqlvalues varchar(8000)
set @sql =' ('
set @sqlvalues = 'values (''+'
select @sqlvalues = @sqlvalues + cols + ' + '','' + ' ,@sql = @sql + '[' + name + '],'
from
(select case
when xtype in (48,52,56,59,60,62,104,106,108,122,127)
then 'case when '+ name +' is null then ''null'' else ' + 'cast('+ name + ' as varchar)'+' end'
when xtype in (58,61)
then 'case when '+ name +' is null then ''null'' else '+''''''''' + ' + 'cast('+ name +' as varchar)'+ '+'''''''''+' end'
when xtype in (36,167)
then 'case when '+ name +' is null then ''null'' else '+''''''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (231)
then 'case when '+ name +' is null then ''null'' else '+'''n'''''' + ' + 'replace('+ name+','''''''','''''''''''')' + '+'''''''''+' end'
when xtype in (175)
then 'case when '+ name +' is null then ''null'' else '+''''''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as char(' + cast(length as varchar) + '))+'''''''''+' end'
when xtype in (239)
then 'case when '+ name +' is null then ''null'' else '+'''n'''''' + ' + 'cast(replace('+ name+','''''''','''''''''''') as char(' + cast(length as varchar) + '))+'''''''''+' end'
else '''null'''
end as cols,name
from syscolumns
where id = object_id(@tablename)
) t
set @sql ='select ''insert into ['+ @tablename + ']' + left(@sql,len(@sql)-1)+') ' + left(@sqlvalues,len(@sqlvalues)-4) + ')'' from '+@tablename
exec (@sql)
end--spgeninsertsql 表名
如何將excel匯入資料庫中
1 開啟企業管理器,開啟要匯入資料的資料庫,在表上按右鍵,所有任務 匯入資料,彈出dts匯入 匯出嚮導,按 下一步 2 選擇資料來源 microsoft excel 97 2000,檔名 選擇要匯入的xls檔案,按 下一步 3 選擇目的 用於sql server 的microsoft ole db提...
如何將資料庫水平切分
在大中型專案中,在資料庫設計的時候,考慮到資料庫最大承受資料量,通常會把資料庫或者資料表水平切分,以降低單個庫,單個表的壓力。這裡介紹兩個專案中常用的資料表切分方法。當然這些方法都是在程式中 使用一定的技巧來路由到具體的表的。首先我們要確認根據什麼來水平切分?在我們的系統 sns 中,使用者的uid...
如何將資料庫水平切分
在大中型專案中,在資料庫設計的時候,考慮到資料庫最大承受資料量,通常會把資料庫或者資料表水平切分,以降低單個庫,單個表的壓力。這裡介紹兩個專案中常用的資料表切分方法。當然這些方法都是在程式中 使用一定的技巧來路由到具體的表的。首先我們要確認根據什麼來水平切分?在我們的系統 sns 中,使用者的uid...