1. 程式人生 > >如何將sqlserver表中的資料匯出sql語句或生成insert into語句

如何將sqlserver表中的資料匯出sql語句或生成insert into語句

drop proc proc_insert
go
create proc proc_insert (@tablename varchar(256))
as
begin
set nocount on
declare @sqlstr varchar(4000)
declare @sqlstr1 varchar(4000)
declare @sqlstr2 varchar(4000)
select @sqlstr='select ''insert '[email protected]
select @sqlstr1=''
select @sqlstr2=' ('
select @sqlstr1= ' values ( ''+'
select @
[email protected]
+col+'+'',''+' ,@[email protected]+name +',' from (select case 
-- when a.xtype =173 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =127 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(20),'+a.name +')'+' end'
when a.xtype =104 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(1),'+a.name +')'+' end'
when a.xtype =175 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =61 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =106 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =62 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =56 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(11),'+a.name +')'+' end'
when a.xtype =60 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =239 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =108 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.xprec+2)+'),'+a.name +')'+' end'
when a.xtype =231 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
when a.xtype =59 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(23),'+a.name +',2)'+' end'
when a.xtype =58 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'convert(varchar(23),'+a.name +',121)'+ '+'''''''''+' end'
when a.xtype =52 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(12),'+a.name +')'+' end'
when a.xtype =122 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(22),'+a.name +')'+' end'
when a.xtype =48 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar(6),'+a.name +')'+' end'
-- when a.xtype =165 then 'case when '+a.name+' is null then ''NULL'' else '+'convert(varchar('+convert(varchar(4),a.length*2+2)+'),'+a.name +')'+' end'
when a.xtype =167 then 'case when '+a.name+' is null then ''NULL'' else '+'''''''''+'+'replace('+a.name+','''''''','''''''''''')' + '+'''''''''+' end'
else '''NULL'''
end as col,a.colid,a.name
from syscolumns a where a.id = object_id(@tablename) and a.xtype <>189 and a.xtype <>34 and a.xtype <>35 and a.xtype <>36
)t order by colid

select @
[email protected]
+left(@sqlstr2,len(@sqlstr2)-1)+') '+left(@sqlstr1,len(@sqlstr1)-3)+')'' from '[email protected]
-- print @sqlstr
exec( @sqlstr)
set nocount off
end
go

select * from systypes

相關推薦

如何sqlserver資料匯出sql語句生成insert into語句

drop proc proc_insert go create proc proc_insert (@tablename varchar(256)) as begin set nocount on declare @sqlstr varchar(4000) declare @sqlstr1 varchar(4

Java資料庫資料匯出至Excel表格

        在我們學習和使用Java的過程中,會有匯出資料庫中的資料(或其他資料)到表格中的需求。比如你建了一個部落格網站,也寫了很多篇部落格,那麼就想把寫的所有部落格匯出到xls表格中。因為資料在伺服器可能會面臨資料丟失的風險(比如伺服器重灌系統,伺服器

利用groovy把資料匯出成txtcsv

膠水語言就是膠水,寫起也挺快的.這個指令碼主要是從資料庫中把表中的資料匯出來生成檔案.而不用每次都開啟資料庫編輯器去手工收集.然後結合強大的指令碼(shell,bat)等基本就可以實現定時生成最新資料文字了.目前這個gr

SQL SERVER匯出資料sql指令碼形式

CREATE proc spGenInsertSQL (@tablename varchar(256)) as begin declare @sql varchar(8000) declare @sqlValues varchar(8000) set @sql =' (' set @sqlVal

SAP 資料匯出到本地xls檔案 abap例項 INCLUDE OLE2INCL

*&---------------------------------------------------------------------* *&      Form  FRM_DOWNLOAD_DATA *&------------------------------------

pythonmysql資料抽取到另一個mysql庫,持續更新抽取到oracle

import MySQLdb import ConfigParser class Mysql2Mysql(object):     def getConn(self,filename,dbname):         cf = ConfigParser.ConfigPars

mysql一個資料插入另一張,排重,刪除,匯入

將一個表中資料插入另一張表的語句: insert into 目標表(欄位1,欄位2,欄位3……) select  欄位1,欄位2,欄位3… from 來源表 where 條件1 and 條件2; 排重很簡單,只要在需要排重的欄位前加distinct就可以嘞!eg~ 查詢記錄

SAP ABAP如何資料匯出到EXCEL

INCLUDE OLE2INCL. "定義OLE變數DATA: EXCEL    TYPE OLE2_OBJECT,       SHEET    TYPE OLE2_OBJECT,       CELL     TYPE OLE2_OBJECT,       WORKBOOK TYPE OLE2_OBJEC

利用poiexcel資料讀取存入mysql資料庫(資料量比較大)

最近被老大安排了一個任務,利用程式將excle表中的資料讀取到,做處理,然後存進資料庫。接到任務的時候人是懵逼的。但是安排的任務也得硬著頭皮完成。現將做的東西記錄如下,方便以後查詢。 這個小demo的原型是在網上找的,demo連結如下 http://www.cnblogs.

使用C#Excel檔案資料匯入SQL Server資料庫

由於專案中加入了新的功能,可以使管理員向資料庫中匯入Excel資料。因此,在商品管理這塊需要對Excel進行操作,在網上查了些資料,根據專案的實際情況進行了一定的優化,這裡簡單的介紹下。 C#程式碼 /// <summary> /// 上傳Excel檔案,並將資

SQL server while迴圈的使用方法,day資料累加到sum

       因為在保險行業工作,在統計資料的時候會遇到將每天的資料累加起來作為年累計的資料落到sum表中。涉及公司的機密就不把實際的表結構和資料展示,我重新建立一些測試表來說明while迴圈的神奇之處,day的資料累加到sum表中。 一、準備資料表t

linux下 mysql清空資料的內容sql語句

truncate gk_portal;   delete from  gk_portal;    這兩者都是將gk_portal表中資料清空,不過也是有區別的,如下: truncate是整體刪除(速度

利用shellmysql資料匯出到檔案和執行mysql語句

利用mysqldump匯出mysql資料 匯出指定條件的資料庫 命令格式 mysqldump -u使用者名稱 -p密碼 -h主機 -P埠 資料庫名 表名 --where "sql語句" > 路徑 示例程式碼 #!/bin/bash #變數定義 host="127.0.

SQL 一個資料插入到另一個

語法: 1.原來沒有的建立的表 select * into 表A form 表B where條件 2.原來存在的表 insert into 表A select * from 表B where 條件 示例: USE Student_Course2 IF EXISTS(SELE

sql一個資料插入到另一個的方法

列名不一定要相同,只要你在HH中列出要插入列的列表跟select from mm表中的選擇的列的列表一一對應就可以了,當然兩邊的資料型別應該是相容的。 insert into hh (fielda,f

如何table資料匯出excel

所用技術:Mybatis+Springmvc+Spring  例子: @RequestMapping(value = "/sbExport") public void sbcscxInquiry

sqlserver怎麼excel資料匯入到資料庫

在資料庫初始階段,我們有些資料在EXCEL中做好之後,需要將EXCEL對應列名(匯入後對應資料庫表的欄位名),對應sheet(改名為匯入資料庫之後的表名)匯入指定資料庫, 相當於匯入一張表的整個資料。匯入之前需要檢查是否存在同名的表,匯入的資料表以“$”結尾就是為了避免表重複,以作區分。下面就來看看具體操作步

資料INSERT INTO 語句輸出

在MS SQL Server 2000中建立一個可以將一個表中的資料以INSERT INTO 語句的方式輸出的儲存過程。 CREATE proc pGetInsertSQL (@TableName varchar(256))asbegin set nocount on dec

Sql Server 一個資料複製到另個一

目標表存在 將table1中資料全部複製到table2中(table1,table2表結構一致) insert into table1  select * from table2 將table1中name為張三的資料複製到table2中(table1,table2表結構

如何使用plsql匯出oracle資料語句,儲存過程,檢視。以及資料

轉載自:http://blog.csdn.net/u010735684/article/details/48524727 侵刪 第一:匯出資料建表語句,儲存過程,檢視等 開啟plsql,如圖,點選工具————匯出使用者物件  點選匯出使用者物件。如圖所示:  如圖勾