Expoprtar datos de tabla a archivo .sql

09/07/2009 - 18:38 por Rosa Mendoza | Informe spam
He encontrado un procedimiento en la web que permite expaortar los datos
de las tablas en archivos .sql, pero el codigo me da error y no puedo
usarlo, se supone que al sp le paso (separados por , ) las tablas que
deseo exportar pero me da error.

Aqui les copio el codigo y gracias por su ayuda.

CREATE PROC sp_DataAsInsCommand (
@TableList varchar (8000))
AS
SET NOCOUNT ON
DECLARE @position int, @exec_str varchar (2000), @TableName varchar (50)
DECLARE @name varchar(128), @xtype int, @status tinyint, @IsIdentity tinyint
SELECT @TableList = @TableList + ','
SELECT @IsIdentity = 0
SELECT @position = PATINDEX('%,%', @TableList)
WHILE (@position <> 0)
BEGIN

SELECT @TableName = SUBSTRING(@TableList, 1, @position - 1)
SELECT @TableList = STUFF(@TableList, 1, PATINDEX('%,%',
@TableList),'')
SELECT @position = PATINDEX('%,%', @TableList)

SELECT @exec_str = 'DECLARE fetch_cursor CURSOR FOR ' + 'SELECT
a.name, a.xtype, a.status FROM syscolumns a, sysobjects b WHERE a.id =
b.id and b.name = ''' + @TableName + ''''
EXEC (@exec_str)
OPEN fetch_cursor
FETCH fetch_cursor INTO @name, @xtype, @status
IF (@status & 0x80) <> 0
BEGIN
SELECT @IsIdentity = 1
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' ON'
SELECT 'GO'
END
SELECT @exec_str = "SELECT 'INSERT INTO " + @TableName + " VALUES
(' + "
Select ' -- The table name is: ' + @TableName
IF (@xtype = 35) OR (@xtype = 99)
SELECT @exec_str = @exec_str + '''"None yet"'''
ELSE

IF (@xtype = 34)
SELECT @exec_str = @exec_str + '"' + '0xFFFFFFFF' + '"'
ELSE

IF (@xtype = 58) OR (@xtype = 61)
SELECT @exec_str = @exec_str + 'Coalesce(' + ' + ''"'' + ' + '
+ CONVERT(varchar,' + @name + ',101)' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 167) OR (@xtype = 175) OR (@xtype = 231) OR (@xtype = 239)
SELECT @exec_str = @exec_str + 'Coalesce(' + '''"'' + ' + @name
+ ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 36)
SELECT @exec_str = @exec_str + ' + Coalesce(''"'' + ' + ' +
CONVERT(varchar(255),' + @name + ')' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 173) OR (@xtype = 165)
SELECT @exec_str = @exec_str + '"' + '0x0' + '"'
ELSE

SELECT @exec_str = @exec_str + 'Coalesce(CONVERT(varchar,' +
@name + '), "null")'

WHILE @@FETCH_STATUS <> -1
BEGIN
FETCH fetch_cursor INTO @name, @xtype, @status
IF (@@FETCH_STATUS = -1) BREAK
IF (@status & 0x80) <> 0
BEGIN
SELECT @IsIdentity = 1
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' ON'
SELECT 'GO'
END

IF (@xtype = 35) OR (@xtype = 99)
SELECT @exec_str = @exec_str + ' + ","' + ' + ''"None yet"'''
ELSE

IF (@xtype = 34)
SELECT @exec_str = @exec_str + ' + "," + ' + '"' +
'0xFFFFFFFF' + '"'
ELSE

IF (@xtype = 58) OR (@xtype = 61)
SELECT @exec_str = @exec_str + ' + ","' + ' + Coalesce(''"''
+ ' + ' + CONVERT(varchar,' + @name + ',101)' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 167) OR (@xtype = 175) OR (@xtype = 231) OR
(@xtype = 239)
SELECT @exec_str = @exec_str + ' + ","' + ' + Coalesce(''"''
+ ' + @name + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 36)
SELECT @exec_str = @exec_str + ' + ","' + ' + Coalesce(''"''
+ ' + ' + CONVERT(varchar(255),' + @name + ')' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 173) OR (@xtype = 165)
SELECT @exec_str = @exec_str + ' + "," + ' + '"' + '0x0' + '"'
ELSE

SELECT @exec_str = @exec_str + ' + ","' + ' +
Coalesce(CONVERT(varchar,' + @name + '), "null")'
END

CLOSE fetch_cursor
DEALLOCATE fetch_cursor

SELECT @exec_str = @exec_str + '+ ")" FROM ' + @TableName
EXEC(@exec_str)
SELECT 'GO'

IF @IsIdentity = 1
BEGIN
SELECT @IsIdentity = 0
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' OFF'
SELECT 'GO'
END
END


Rosa Mendoza
edberlith@gmail.com
 

Leer las respuestas

#1 Maxi
09/07/2009 - 21:23 | Informe spam
Hola, mirate este post mio que creo te puede ayudar a hacer la cosa mas
simple :)

http://blog.maxiaccotto.com/post/20...tudio.aspx





Maxi Accotto
Microsoft MVP en SQL Server
Consultor en SQL Server
http://blog.maxiaccotto.com



"Rosa Mendoza" escribió en el mensaje de
noticias:#
He encontrado un procedimiento en la web que permite expaortar los datos
de las tablas en archivos .sql, pero el codigo me da error y no puedo
usarlo, se supone que al sp le paso (separados por , ) las tablas que
deseo exportar pero me da error.

Aqui les copio el codigo y gracias por su ayuda.

CREATE PROC sp_DataAsInsCommand (
@TableList varchar (8000))
AS
SET NOCOUNT ON
DECLARE @position int, @exec_str varchar (2000), @TableName varchar (50)
DECLARE @name varchar(128), @xtype int, @status tinyint, @IsIdentity
tinyint
SELECT @TableList = @TableList + ','
SELECT @IsIdentity = 0
SELECT @position = PATINDEX('%,%', @TableList)
WHILE (@position <> 0)
BEGIN

SELECT @TableName = SUBSTRING(@TableList, 1, @position - 1)
SELECT @TableList = STUFF(@TableList, 1, PATINDEX('%,%',
@TableList),'')
SELECT @position = PATINDEX('%,%', @TableList)

SELECT @exec_str = 'DECLARE fetch_cursor CURSOR FOR ' + 'SELECT
a.name, a.xtype, a.status FROM syscolumns a, sysobjects b WHERE a.id =
b.id and b.name = ''' + @TableName + ''''
EXEC (@exec_str)
OPEN fetch_cursor
FETCH fetch_cursor INTO @name, @xtype, @status
IF (@status & 0x80) <> 0
BEGIN
SELECT @IsIdentity = 1
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' ON'
SELECT 'GO'
END
SELECT @exec_str = "SELECT 'INSERT INTO " + @TableName + " VALUES (' +
"
Select ' -- The table name is: ' + @TableName
IF (@xtype = 35) OR (@xtype = 99)
SELECT @exec_str = @exec_str + '''"None yet"'''
ELSE

IF (@xtype = 34)
SELECT @exec_str = @exec_str + '"' + '0xFFFFFFFF' + '"'
ELSE

IF (@xtype = 58) OR (@xtype = 61)
SELECT @exec_str = @exec_str + 'Coalesce(' + ' + ''"'' + ' + ' +
CONVERT(varchar,' + @name + ',101)' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 167) OR (@xtype = 175) OR (@xtype = 231) OR (@xtype =
239)
SELECT @exec_str = @exec_str + 'Coalesce(' + '''"'' + ' + @name +
' + ''"''' + ',"null")'
ELSE

IF (@xtype = 36)
SELECT @exec_str = @exec_str + ' + Coalesce(''"'' + ' + ' +
CONVERT(varchar(255),' + @name + ')' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 173) OR (@xtype = 165)
SELECT @exec_str = @exec_str + '"' + '0x0' + '"'
ELSE

SELECT @exec_str = @exec_str + 'Coalesce(CONVERT(varchar,' + @name
+ '), "null")'

WHILE @@FETCH_STATUS <> -1
BEGIN
FETCH fetch_cursor INTO @name, @xtype, @status
IF (@@FETCH_STATUS = -1) BREAK
IF (@status & 0x80) <> 0
BEGIN
SELECT @IsIdentity = 1
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' ON'
SELECT 'GO'
END

IF (@xtype = 35) OR (@xtype = 99)
SELECT @exec_str = @exec_str + ' + ","' + ' + ''"None yet"'''
ELSE

IF (@xtype = 34)
SELECT @exec_str = @exec_str + ' + "," + ' + '"' + '0xFFFFFFFF'
+ '"'
ELSE

IF (@xtype = 58) OR (@xtype = 61)
SELECT @exec_str = @exec_str + ' + ","' + ' + Coalesce(''"'' +
' + ' + CONVERT(varchar,' + @name + ',101)' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 167) OR (@xtype = 175) OR (@xtype = 231) OR (@xtype =
239)
SELECT @exec_str = @exec_str + ' + ","' + ' + Coalesce(''"'' +
' + @name + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 36)
SELECT @exec_str = @exec_str + ' + ","' + ' + Coalesce(''"'' +
' + ' + CONVERT(varchar(255),' + @name + ')' + ' + ''"''' + ',"null")'
ELSE

IF (@xtype = 173) OR (@xtype = 165)
SELECT @exec_str = @exec_str + ' + "," + ' + '"' + '0x0' + '"'
ELSE

SELECT @exec_str = @exec_str + ' + ","' + ' +
Coalesce(CONVERT(varchar,' + @name + '), "null")'
END

CLOSE fetch_cursor
DEALLOCATE fetch_cursor

SELECT @exec_str = @exec_str + '+ ")" FROM ' + @TableName
EXEC(@exec_str)
SELECT 'GO'

IF @IsIdentity = 1
BEGIN
SELECT @IsIdentity = 0
SELECT 'SET IDENTITY_INSERT ' + @TableName + ' OFF'
SELECT 'GO'
END
END


Rosa Mendoza

Preguntas similares