dynamically defining table in a view definition

  • Can anyone help. I am trying to define a table name dynamically with a view definition.

    For example

    Select * FROM SMSLogmmyyy

    where mm is the current month and yyyy is current year.

    I have tables of data called

    SMSLog012002

    SMSLog022002

    and I am trying to create view called CurrentMonthView

    something like

    @TableName = 'SMSLog' & month(getdate()) & year(getdate())

    Select * from @TableName

    Thanks

    Tim.

  • As far as I can tell you can't declare a variable in a view.

    Also you can't execute a query like this:

    Select * from @TableName

    To do something like the above you would need to create the query in a variable and then execute the variable. I don't believe you can do this in a view.

    Robert Marda

    Robert W. Marda
    Billing and OSS Specialist - SQL Programmer
    MCL Systems

  • Roberts right, views do not support variables in any way. You will have to use a Stored Procedure or create a script of code but views are out.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

  • Vote 3 = Can't be done

    Steve Jones

    sjones@sqlservercentral.com

    http://www.sqlservercentral.com/columnists/sjones

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply