A script that generates data from a table.

Source: Internet
Author: User
Tags end insert sql rtrim
Scripts | data
In a server has a table table1, want to take the contents of the table to the B server, these two databases are not networked, how to do?
Lead to external file, then export with import .... This is too stupid, if the other server is on the Internet, can only do the implementation of the clothes, Dizzy dead bar, with this stored procedure to help you.
ALTER proc Sp_gettabledata
@objname nvarchar (776)
As
--Set @objname = ' S_coderule '

/* Declare @no varchar (@yes varchar), @none varchar (35)
Select @no = name from master.dbo.spt_values where type = ' B ' and number = 0
Select @yes = name from master.dbo.spt_values where type = ' B ' and number = 1
Select @none = name from master.dbo.spt_values where type = ' B ' and number = 2
*/
declare @numtypes nvarchar (80)
Select @numtypes = N ' Tinyint,smallint,decimal,int,real,money,float,numeric,smallmoney,varbinary,bit '
declare @chartypes nvarchar (80)
Select @chartypes = N ' Char,varchar,text,nchar,ntext,nvarchar '
declare @datetypes nvarchar (80)
Select @datetypes = N ' Datetimesmalldatetime '
declare @Limitlength int, @Limitlength1 int

DECLARE @objid bigint
--declare @sysobj_type char (2)
Select @objid = ID
--, @sysobj_type = xtype
from sysobjects where id = object_id (@objname)
Set @Limitlength = 0
Set @limitLength1 = 0
DECLARE @name varchar (), @type varchar (a), @length int, @FieldSQL varchar (5000), @SQL varchar (8000), @ident smallint
Set @SQL = '
Set @FieldSQL = '

DECLARE cur cursor
For

Select
' column_name ' = name,
' Type ' = type_name (Xusertype),
' Length ' = convert (int, Length),
' Ident ' =colstat
From syscolumns where id = @objid and number = 0 ORDER by colid

Open Cur
FETCH NEXT from cur into @name, @type, @length, @ident
While @ @fetch_status =0
Begin
If @Ident =0
Begin
If @Limitlength > 200
Begin
Set @sql = @sql + ', '
Set @limitlength = 0
End

If @Limitlength1 > 200
Begin
Set @Fieldsql = @Fieldsql + char (+) + ', ' + char (39)
Set @limitlength1 = 0
End
Set @FieldSQl = @FieldSQL + LTrim (Rtrim (@name)) + ', '
If CHARINDEX (@type, @numtypes) >0--(' +convert (char (6), @length) + ')
Set @sql = @SQL + ' + case when ' + @name + ' are null Then ' ' null ' else Rtrim (Convert (char, ' + @name + ')) End '
else if CHARINDEX (@type, @chartypes) >0
Set @sql = @SQL + ' + case when ' + @name + ' is null then ' null ' else +char (+) + RTrim (' + @name + ') +char
else if CHARINDEX (@type, @datetypes) >0
Set @sql = @SQL + ' + case ' + @name + ' is null then ' ' null ' else +char (+) + RTrim (convert (char), ' + @name + ',) +char (39 ) End '
Set @SQL = @SQL + ' + ' +char (+) + ', ' +char (39)
Set @Limitlength = @Limitlength + @Length
Set @Limitlength1 = @Limitlength1 + Len (@Name)

End

FETCH NEXT from cur into @name, @type, @length, @ident
End
Close cur
Deallocate cur

--Print @sql
If Right (@FieldSQl, 1) = char (39)
SET @FieldSQL = Substring (@FieldSQL, 1, Len (@FieldSQL)-5)
Else
SET @FieldSQL = Substring (@FieldSQL, 1, Len (@FieldSQL)-1)

If Right (@SQl, 1) = ', '
SET @SQL = Substring (@SQL, 1, Len (@SQL)-5)
Else
SET @SQL =substring (@SQL, 1,len (@SQL)-4)

--print (@FieldSQL)



Set @FieldSQl = ' SELECT ' +char + ' insert INTO ' + @objname + ' (' + @FieldSQL + ') '
--set @FieldSQl = ' SELECT ' +char + ' insert INTO ' + @objname + ' (' + @FieldSQL + ') '
Print @FieldSQL

Set @Sql = ' VALUES (' +char + @SQL + ' + ' +char () + ') ' +char + ' from ' + @objname

Print @SQL

Set @sql = @fieldsql + @sql
EXEC (@sql)

--exec @FieldSQl + @sql
--select Rtrim (IsNull (CONVERT (char (8), Did), ")" from S_send_files

Go
SET QUOTED_IDENTIFIER OFF
Go
SET ANSI_NULLS on
Go

Look at the results, there are some bugs, enough on the line, too lazy to change.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.