1. 程式人生 > >將表中資料以INSERT INTO 語句輸出

將表中資料以INSERT INTO 語句輸出

在MS SQL Server 2000中建立一個可以將一個表中的資料以INSERT INTO 語句的方式輸出的儲存過程。

CREATE proc pGetInsertSQL (@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 =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
 

相關推薦

資料INSERT INTO 語句輸出

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

根據MySQL資料匯出INSERT INTO語句的方法

因專案中有需要需根據MySQL表資料生成INSERT INTO語句,在網上找了些現成的程式碼,原作者是誰就不知道了,但是發現有BUG,不能適用,遂對他人程式碼進行了修改。修改後能較好的匯出INSERT INTO語句。程式碼如下: package dwz.interactio

c語言 記憶體資料二進位制形式寫入檔案 檔案資料表現形式

最近有在寫關於將記憶體中的資料寫入檔案的程式,當程式執行後,卻發現檔案中的位元組資料有些難以理解。思考後發現了其中的道理。 程式碼如下: #include<stdio.h> #include<stdlib.h> struct BlockInfo { bool is

如何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表格中。因為資料在伺服器可能會面臨資料丟失的風險(比如伺服器重灌系統,伺服器

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

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

資料複製到另一個數據庫的

1. 在 src 目錄下建立 jdbc.properties #Oracle oracle.driver=oracle.jdbc.driver.OracleDriver oracle.jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl

Hiveinsert into語句

hive中的語句有兩種: insert into table cite select * from cite; 這個的意思就是將cite表中的資料複製一份,然後插入到原表中去,而 inse

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~ 查詢記錄

利用SQL語句的case、when、sum實現的一列變為多列

一、原表status的結構如圖所示:展示的主要欄位有addtime1(丟失時間)、addtime2(撿到時間)、status(狀態)1,表示成功;2 表示認領中;0表示未認領;二、現在報表設計的需求是統計一個月之內成功認領的個數、兩個月之內成功認領的個數、三個月之內成功認領以

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

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

MY/SQL_誤刪除DROP,或刪除資料DELETE還原

1.DROP刪除表找回 --查詢回收站 SELECT * FROM RECYCLEBIN; --回收站裡存在被刪除表,恢復表(h表) flashback table h to before drop ; --查詢表 SELECT * FROM h; --如果想徹底刪

mysqlinsert into語句的5種寫法(上)

insert into是mysql中最常用的插入語句,它有5種寫法。下面讓我們一起來看一看吧: http://blog.csdn.net/number1killer/article/details/778424721向原表中某些欄位中插入一條記錄。語法:insert into

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

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

左.右連線相關 , table1資料匯入到table2(結構不相同)

 現在要將table1中的資料轉到table2中,請教SQL語句該如何實現(只需要查詢語句就行了); 表   table1 name time    value     type A 2011-6-13     51       1 B 2011-6-13     

ORACLE資料恢復到某一個時間點

執行如下SQL將test_temp表中的資料恢復到 2011 04 29  11:06:00 注意,這裡一定要先刪除全部資料,否則可能會導致資料重複或者新建一個表結構和test_temp結構相同的表,把資料匯入到新表中 delete from test_temp; ins

vertica從其它遷移數據到新insert into 語句使用方法實例)

clas -h 語句 use desc lower weibo name pos 版權聲明:本文為博主原創文章。博主同意

怎麽的空格都轉變為0???

輸入 blog .com 替換 查找 ron 全部 ges mage 一、進入excel表格,選擇需要轉換的範圍。 二、點擊右上方的‘查找和替換’中的‘替換’。 三、將‘查找內容’後輸入空格,‘替換’框輸入‘0’,然後點擊‘全部替換’,就OK了。 怎麽將表中的空格都

Insert Into 語句的語法錯誤

img ins height display 數據 play 技術 wid info 錯誤示意: 一開始程序是: 改正: 一條很簡單的插入語句竟然會報錯,然而直接在數據庫的查詢中執行中卻沒有問題,這個問題困擾了不少時間。 數據庫使用的是ACCESS,INSERT IN