none
Gerar script SQL RRS feed

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

    go

    from: http://www.nigelrivett.net/SQLTsql/sp_CreateDataLoadScript.html

     

    Espero que te ajude

    abs

    FC
    http://sushinetcode.blogdrive.com

     

    quinta-feira, 1 de março de 2007 23:50

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

    go

    from: http://www.nigelrivett.net/SQLTsql/sp_CreateDataLoadScript.html

     

    Espero que te ajude

    abs

    FC
    http://sushinetcode.blogdrive.com

     

    quinta-feira, 1 de março de 2007 23:50
  • Oi Fernando, blz?

    Tem que fazer tudo isso só pra gerar o script de inserção do BD??

    Não tem nada no Enterprise Manager tipo aquele Generate SQL que gera o script das tabelas e procedures??

    Valeu.

    sexta-feira, 2 de março de 2007 12:27
  • Bom dia Fernando vc deseja selecionar as tabelas de um banco de dados e gerar um script dos registros contidos nesta tabela? é isso ?
    sexta-feira, 2 de março de 2007 13:39
  • Oi Anderson.

    Isso mesmo o script do Insert que foi feito para todos os dados de uma determinada tabela.

    Abç,

    Fernando.

    sexta-feira, 2 de março de 2007 13:56
  • 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

    sexta-feira, 2 de março de 2007 14:09
  • 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 <=@Linhas

    Begin

    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 + 1

    Exec(@comando)

    End


    -- Aqui executa
    Exec P_MontarTextos 1000

    -- aqui consulta
    Select * from Textos

    Espero ter ajudado!

    quinta-feira, 8 de março de 2007 15:23