• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!
Collapse

You are not logged in or you do not have permission to access this page. This could be due to one of several reasons:

  • You are not logged in. If you are already registered, fill in the form below to log in, or follow the "Sign Up" link to register a new account.
  • You may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
  • If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation.

Previously on "Build Tool for SQL Script"

Collapse

  • Spacecadet
    replied
    Originally posted by PorkPie View Post

    I would like to be able, for example, to define a table in one file, and then import it in another that requires the table to be present to compile.
    Are you using SQL Server?
    Tables don't need to exist for a procedure to compile:
    Code:
    create procedure deferred_test
    as
    begin
    select * from dofkdofkdokf
    end
    should compile fine

    Leave a comment:


  • PorkPie
    replied
    Originally posted by Jaws View Post
    Do you have visual studio installed? There is a command line utility for transforming T4 templates (which support includes) into files. I believe it's available from the VS command line.

    The utility is called "TextTransform.exe" and you'd include <#@ include file="table.sql" #> in your files.

    I've used it with great success to generate a heap of SQL code in the past.
    Cool thanks - will take a look

    Leave a comment:


  • Jaws
    replied
    Originally posted by PorkPie View Post
    Thanks all,

    Perhaps I should make this more generic. All I want to do is include stuff from one file with stuff from a build file to generate a merged output.

    ---------file 1---------

    stuff to include

    blah blah blah

    -----------------------

    -------- file 2 skeleton ---------

    <include file 1>

    do stuff in file 2

    foo bar foo bar

    ---------- end of file -----------

    >> Run build

    ------------ resulting file --------

    stuff to include

    blah blah blah

    do stuff in file 2

    foo bar foo bar


    ----------end resulting file -------------

    does that make sense?

    Do you have visual studio installed? There is a command line utility for transforming T4 templates (which support includes) into files. I believe it's available from the VS command line.

    The utility is called "TextTransform.exe" and you'd include <#@ include file="table.sql" #> in your files.

    I've used it with great success to generate a heap of SQL code in the past.

    Leave a comment:


  • PorkPie
    replied
    Thanks all,

    Perhaps I should make this more generic. All I want to do is include stuff from one file with stuff from a build file to generate a merged output.

    ---------file 1---------

    stuff to include

    blah blah blah

    -----------------------

    -------- file 2 skeleton ---------

    <include file 1>

    do stuff in file 2

    foo bar foo bar

    ---------- end of file -----------

    >> Run build

    ------------ resulting file --------

    stuff to include

    blah blah blah

    do stuff in file 2

    foo bar foo bar


    ----------end resulting file -------------

    does that make sense?

    Leave a comment:


  • Jaws
    replied
    Assuming you are using SQL server:

    You can run scripts built in that way using sqlcmd.exe. For instance you can include variables and import files.

    For example you can use :r <path> in the script to include a file.

    http://msdn.microsoft.com/en-us/library/ms162773.aspx

    I'd guess SQLCMD does not generate an output SQL script - but it could be an option if you can use it to deploy to the DB.

    Leave a comment:


  • mudskipper
    replied
    Originally posted by PorkPie View Post
    I am trying to refactor a load of spaghetti code in some SQL I've inherited. Currently it's in one massive script with loads of temp tables and and interdependencies. I want to split this out into manageable files to start to understand the dependencies and make the source code easier to manage.

    I would like to be able, for example, to define a table in one file, and then import it in another that requires the table to be present to compile. E.g.

    -- File 1 table defs -------------------------------
    -- table definition
    TABLE_1 [
    create table T (id int, name varchar(20))
    go
    ]

    -- End File 1 -------------------------------------

    -- File 2 -- sp_FirstProc

    [INSERT TABLE_1]

    create procedure sp_FirstProc
    @myId int
    as
    begin

    select * from T where id=@myId

    end

    ---- end file -------------------------

    when I run the build script it replaces the tokens in the second file with the one defined in the first to produce:

    --- file created by build script--------

    create table T (id int, name varchar(20))
    go

    create procedure sp_FirstProc
    @myId int
    as
    begin

    select * from T where id=@myId

    end

    --- end file created by build script--------


    Any suggestions? Thanks!
    Can't think how you'd actually include another script, but you could stick it in a sp and call that.

    If you're trying to achieve a variable table name, you'll probably need to use dynamic SQL

    It might be worth checking out common table expressions - they can be useful for simplifying SQL - especially if you have something like a similar subselect being called multiple times.

    Leave a comment:


  • TheFaQQer
    replied
    Script generate.sql:
    Code:
    def table_name = &1
    
    create table &table_name ( col1 varchar2(100), col2 number)
    /
    
    create or replace procedure MYPROC&table_name AS
      v1 varchar2(100);
    begin
      select col1 into v1 from &table_name where rownum = 1;
    end;
    /
    then @generate <table name>

    works in Oracle - dunno if other databases allow the same flexibility, though.

    Leave a comment:


  • PorkPie
    started a topic Build Tool for SQL Script

    Build Tool for SQL Script

    I am trying to refactor a load of spaghetti code in some SQL I've inherited. Currently it's in one massive script with loads of temp tables and and interdependencies. I want to split this out into manageable files to start to understand the dependencies and make the source code easier to manage.

    I would like to be able, for example, to define a table in one file, and then import it in another that requires the table to be present to compile. E.g.

    -- File 1 table defs -------------------------------
    -- table definition
    TABLE_1 [
    create table T (id int, name varchar(20))
    go
    ]

    -- End File 1 -------------------------------------

    -- File 2 -- sp_FirstProc

    [INSERT TABLE_1]

    create procedure sp_FirstProc
    @myId int
    as
    begin

    select * from T where id=@myId

    end

    ---- end file -------------------------

    when I run the build script it replaces the tokens in the second file with the one defined in the first to produce:

    --- file created by build script--------

    create table T (id int, name varchar(20))
    go

    create procedure sp_FirstProc
    @myId int
    as
    begin

    select * from T where id=@myId

    end

    --- end file created by build script--------


    Any suggestions? Thanks!

Working...
X