zoukankan      html  css  js  c++  java
  • Sqlserver 导出insert插入语句

    1. SET QUOTED_IDENTIFIER OFF   
    2. GO  
    3. SET ANSI_NULLS ON   
    4. GO  
    5. if exists(select 1 from sysobjects where id=object_id('BicashyOutputData'and xtype='P')     
    6.    drop procedure BicashyOutputData;  
    7. GO  
    8. create procedure dbo.BicashyOutputData(@tablename varchar(256),@whereStr varchar(256))  
    9. AS    
    10. declare @column varchar(1000)    
    11. declare @columndata varchar(1000)    
    12. declare @sql varchar(4000)    
    13. declare @xtype tinyint    
    14. declare @name sysname    
    15. declare @objectId int    
    16. declare @objectname sysname    
    17. declare @ident int    
    18.   
    19. set nocount on    
    20. set @objectId=object_id(@tablename)    
    21.   
    22. if @objectId is null -- 判斷對象是否存在    
    23. begin    
    24. print 'The object not exists'    
    25. return    
    26. end    
    27. set @objectname=rtrim(object_name(@objectId))    
    28.   
    29. if @objectname is null or charindex(@objectname,@tablename)=0 --此判断不严密    
    30. begin    
    31. print 'object not in current database'    
    32. return    
    33. end    
    34.   
    35. if OBJECTPROPERTY(@objectId,'IsTable') < > 1 -- 判斷對象是否是table    
    36. begin    
    37. print 'The object is not table'    
    38. return    
    39. end    
    40.   
    41. select @ident=status&0x80 from syscolumns where id=@objectid and status&0x80=0x80    
    42.   
    43. if @ident is not null    
    44. print 'SET IDENTITY_INSERT '+@TableName+' ON'    
    45.   
    46. declare syscolumns_cursor cursor  
    47.   
    48. for select c.name,c.xtype from syscolumns c where c.id=@objectid order by c.colid    
    49.   
    50. open syscolumns_cursor    
    51. set @column=''    
    52. set @columndata=''    
    53. fetch next from syscolumns_cursor into @name,@xtype    
    54.   
    55. while @@fetch_status < >-1    
    56. begin    
    57. if @@fetch_status < >-2    
    58. begin    
    59. if @xtype not in(189,34,35,99,98) --timestamp不需处理,image,text,ntext,sql_variant 暂时不处理    
    60.   
    61. begin    
    62. set @column=@column+case when len(@column)=0 then'' else ','end+@name    
    63.   
    64. set @columndata=@columndata+case when len(@columndata)=0 then '' else ','','','  
    65. end    
    66.   
    67. +case when @xtype in(167,175) then '''''''''+'+@name+'+''''''''' --varchar,char    
    68. when @xtype in(231,239) then '''N''''''+'+@name+'+''''''''' --nvarchar,nchar    
    69. when @xtype=61 then '''''''''+convert(char(23),'+@name+',121)+''''''''' --datetime    
    70. when @xtype=58 then '''''''''+convert(char(16),'+@name+',120)+''''''''' --smalldatetime    
    71. when @xtype=36 then '''''''''+convert(char(36),'+@name+')+''''''''' --uniqueidentifier    
    72. else @name end    
    73.   
    74. end    
    75.   
    76. end    
    77.   
    78. fetch next from syscolumns_cursor into @name,@xtype    
    79.   
    80. end    
    81.   
    82. close syscolumns_cursor    
    83. deallocate syscolumns_cursor    
    84.   
    85. set @sql='set nocount on select ''insert '+@tablename+'('+@column+') values(''as ''--'','+@columndata+','');'' from '+@tablename+' '+@whereStr    
    86.   
    87. print '--'+@sql    
    88. exec(@sql)    
    89.   
    90. if @ident is not null    
    91. print 'SET IDENTITY_INSERT '+@TableName+' OFF'    
    92.   
    93.   
    94. GO  
    95. SET QUOTED_IDENTIFIER OFF   
    96. GO  
    97. SET ANSI_NULLS ON   
    98. GO  

    建立好存储过程以后,可以调用存储过程查看生成好的insert 插入语句。

         具体操作:

         执行 exec BicashyOutputData cardinfo,'where drawOutper="李佩娟"'

         注意cardinfo是要执行的表名,'where drawOutper="李佩娟"' 是where条件,如果不需要查询条件全部导出,则可以直接写为: exec BicashyOutputData cardinfo,''

         还需要注意的一点就是如查询条件中含有字符串,需要在字符串前后加 “”

         最后将生成的记录,全部拷贝出来即可。

  • 相关阅读:
    Python之从头开始建立项目流程
    Python之建立APP流程以及SVN 的使用
    python之继承
    Python之实例对象的增删改查
    Python之类属性的增删改查
    read big file
    python minus 3 days or n days
    movie
    pyqt convert ui file to py file
    pyqt4 borderless window
  • 原文地址:https://www.cnblogs.com/andy24/p/2984485.html
Copyright © 2011-2022 走看看