Usuário com melhor resposta
Gerar script SQL

Pergunta
-
Respostas
-
Crie a proc abaixo e execute o procedimento informando o nome da tabela.
if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_CreateDataLoadScript]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_CreateDataLoadScript]
GO
Create Procedure sp_CreateDataLoadScript
@TblName varchar(128)
as
/*
exec sp_CreateDataLoadScript 'MyTable'
*/
create table #a (id int identity (1,1), ColType int, ColName varchar(128))
insert #a (ColType, ColName)
select case when DATA_TYPE like '%char%' then 1 else 0 end ,
COLUMN_NAME
from information_schema.columns
where TABLE_NAME = @TblName
order by ORDINAL_POSITION
if not exists (select * from #a)
begin
raiserror('No columns found for table %s', 16,-1, @TblName)
return
end
declare @id int ,
@maxid int ,
@cmd1 varchar(7000) ,
@cmd2 varchar(7000)
select @id = 0 ,
@maxid = max(id)
from #a
select @cmd1 = 'select '' insert ' + @TblName + ' ( '
select @cmd2 = ' + '' select '' + '
while @id < @maxid
begin
select @id = min(id) from #a where id > @id
select @cmd1 = @cmd1 + ColName + ','
from #a
where id = @id
select @cmd2 = @cmd2
+ ' case when ' + ColName + ' is null '
+ ' then ''null'' '
+ ' else '
+ case when ColType = 1 then ''''''''' + ' + ColName + ' + ''''''''' else 'convert(varchar(20),' + ColName + ')' end
+ ' end + '','' + '
from #a
where id = @id
end
select @cmd1 = left(@cmd1,len(@cmd1)-1) + ' ) '' '
select @cmd2 = left(@cmd2,len(@cmd2)-8) + ' from ' + @tblName
select '/*' + @cmd1 + @cmd2 + '*/'
exec (@cmd1 + @cmd2)
drop table #a
gofrom: http://www.nigelrivett.net/SQLTsql/sp_CreateDataLoadScript.html
Espero que te ajude
abs
FC
http://sushinetcode.blogdrive.com
Todas as Respostas
-
Crie a proc abaixo e execute o procedimento informando o nome da tabela.
if exists (select * from sysobjects where id = object_id(N'[dbo].[sp_CreateDataLoadScript]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_CreateDataLoadScript]
GO
Create Procedure sp_CreateDataLoadScript
@TblName varchar(128)
as
/*
exec sp_CreateDataLoadScript 'MyTable'
*/
create table #a (id int identity (1,1), ColType int, ColName varchar(128))
insert #a (ColType, ColName)
select case when DATA_TYPE like '%char%' then 1 else 0 end ,
COLUMN_NAME
from information_schema.columns
where TABLE_NAME = @TblName
order by ORDINAL_POSITION
if not exists (select * from #a)
begin
raiserror('No columns found for table %s', 16,-1, @TblName)
return
end
declare @id int ,
@maxid int ,
@cmd1 varchar(7000) ,
@cmd2 varchar(7000)
select @id = 0 ,
@maxid = max(id)
from #a
select @cmd1 = 'select '' insert ' + @TblName + ' ( '
select @cmd2 = ' + '' select '' + '
while @id < @maxid
begin
select @id = min(id) from #a where id > @id
select @cmd1 = @cmd1 + ColName + ','
from #a
where id = @id
select @cmd2 = @cmd2
+ ' case when ' + ColName + ' is null '
+ ' then ''null'' '
+ ' else '
+ case when ColType = 1 then ''''''''' + ' + ColName + ' + ''''''''' else 'convert(varchar(20),' + ColName + ')' end
+ ' end + '','' + '
from #a
where id = @id
end
select @cmd1 = left(@cmd1,len(@cmd1)-1) + ' ) '' '
select @cmd2 = left(@cmd2,len(@cmd2)-8) + ' from ' + @tblName
select '/*' + @cmd1 + @cmd2 + '*/'
exec (@cmd1 + @cmd2)
drop table #a
gofrom: http://www.nigelrivett.net/SQLTsql/sp_CreateDataLoadScript.html
Espero que te ajude
abs
FC
http://sushinetcode.blogdrive.com -
-
-
-
Fernando com exceção de ferramentas de terceiros vc não tem esta funcionalidade no SQL Server,sugiro vc utilizar o exemplo do nosso amigo, caso não queira, basta vc criar o seu próprio script. Para isto basta utilizar a tabela sys.columns (SQL Server 2005) ou syscolumns (SQL Server 2000) para pegar os campos de uma determinada tabela,dar um select na sua tabela registro a registro e a partir daí ir concatenado os campos.Qualquer coisa retorne.
Espero ter ajudado
-
Cara tenho este script que cria uma tbl depois uma sp e depois vc so informa a quantidade de linha que vc deseja inserir...segue
Create Table Textos
(Codigo Int Identity(1,1),
Valor1 VarChar(20),
Valor2 VarChar(20),
Valor3 VarChar(25))
--> Procedure para rodar o número de linhas escolhidas pelo usuário.
Create Procedure P_MontarTextos @Linhas Int
As
Declare @String VarChar(100),
@Comando VarChar(100),
@Contador Int
Set @Contador=1
Truncate Table Textos
While @Contador <=@LinhasBegin
Set @String='''Olá'+Convert(VarChar(4), @Contador)+''''+','+'''Oi'+Convert(VarChar(4), @Contador)+''''+','+'''Tudo Bem'+Convert(VarChar(4), @Contador)+''''
Set @Comando= 'Insert Into Textos Values('+@String+')'
print @comando
Set @Contador = @Contador + 1Exec(@comando)
End
-- Aqui executa
Exec P_MontarTextos 1000-- aqui consulta
Select * from TextosEspero ter ajudado!
- Sugerido como Resposta Junior Galvão - MVPMVP sábado, 22 de junho de 2019 13:50