开发者

TSQL substitution of key words and code blocks

I have blocks of TSQL that I want to create a MACRO for and then reuse in my SQL file. I want this to be a 'compile' time thing only.

Eg:

?set? COMMON = "Field1 int, Field2 char(1),";
?set? MAKEONE = "create table";

MAKEONE XXX (
    COMMON
    Field3 int
);

Please dont ask why I would want to ... :)

... it is for SQL Server.

Ok, what about con开发者_开发百科ditional execution of SQL:

?set? ISYES = true;

?if? ISYES 
create table AAA (...)
?else?
create table BBB (...)


What you are asking makes little sense in SQL terms

Based on your examples:

  1. A CREATE TABLE is exactly that: a CREATE TABLE. Why Macro it? You aren't going to substitute "CREATE PROCEDURE".

  2. Having "common" fields would indicate poor design

You also have to consider:

  • constraints, keys and indexes
  • permissions of using dynamic SQL
  • the cost of developing a "framework" to do what SQL already does
  • permissions of your objects

Now, what is the business problem you are trying to solve?
Instead of asking about your chosen solution...

Edit: question updated as I typed above:

IF (a condition)
   EXEC ('CREATE TABLE ...')
ELSE IF (a condition)
   EXEC ('CREATE TABLE ...')
...

Note that much of DDL in SQL must be in it's own batch or the first statement in a batch. Hence use of dynamic SQL again

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜