Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8982949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:42:31+00:00 2026-06-15T20:42:31+00:00

In a SQL server database I have have this kind of table : EVENT201201.dbo.EvAcc

  • 0

In a SQL server database I have have this kind of table :

EVENT201201.dbo.EvAcc
EVENT201202.dbo.EvAcc
EVENT201203.dbo.EvAcc
EVENT201204.dbo.EvAcc
EVENT201205.dbo.EvAcc
...
EVENTYYYYMM.dbo.EvAcc

I also have DBACCES1.dbo.*. I created a query like this :

DECLARE @SQL varchar(511)
DECLARE @SQL_ACC varchar(250)
DECLARE @SQL_UClass varchar(250)
DECLARE @SQL_UClassDef varchar(250)
DECLARE @TABLENAME varchar(250)


SELECT @TABLENAME ='EVENT' + CONVERT(char(4), YEAR(GETDATE() - 1)) + CONVERT(char(2), MONTH(GETDATE() - 1))
SELECT @SQL_ACC = '' + QuoteName(@TABLENAME) + '.dbo.EvAcc ON EvAcc.fpointeur = cards.fpointeur'
SELECT @SQL_UClass = 'dbacces1.dbo.UClass ON UClass.fpointeur = cards.fpointeur AND MClass = 1'
SELECT @SQL_UClassDef = 'dbacces1.dbo.UClassDef ON UClassDef.SClass = UClass.SClass'

SELECT @SQL = 'SELECT cards.FPointeur, Ref, Peri, cout, cin, edate FROM dbacces1.dbo.cards INNER JOIN ' + @SQL_ACC + ' INNER JOIN ' + @SQL_UClass

EXEC(@SQL)

It works but I need to put this in a view. It doesn’t work. I have this error (sorry for the french) :

La construction ou l'instruction SQL Déclarer un curseur n'est pas prise en charge.

It say than declare is not allowed in a view.

I tried to create a valued table function and it doen’t work because I can’t use the function exec with a string in a function. I have this error :

Invalid use of side-effecting or time-dependent operator in 'EXECUTE STRING' within a function.

I also tried to create procedure but I can’t use it as a table in my query. I tried to create a function which return the database name only but it doen’t work again.

Do you have any solution?

EDIT 1 – Solution

I create a script executed each month. This is my script :

DECLARE @start_date DATETIME
DECLARE @end_date DATETIME
DECLARE @sql VARCHAR(MAX)
DECLARE @dbname VARCHAR(15)
DECLARE @year VARCHAR(4)
DECLARE @month VARCHAR(2)

--Start the SQL request
SET @sql = 'CREATE VIEW dbo.viewEvAcc AS '

SET @start_date = convert(DATETIME, '01/01/2011', 101)
SET @end_date = GETDATE()

--Loop from the start date to now
WHILE @start_date < @end_date
BEGIN
    --Find new year and month
    SET @year = CONVERT(CHAR(4), YEAR(@start_date))
    SET @month = RIGHT('0' + RTRIM(MONTH(@start_date)), 2);

    --Create the db name with year and month
    SET @dbname = 'EVENT' + @year + @month

    --Concat the SQL Request
    SET @sql = @sql + 'SELECT * FROM ' + @dbname + '.dbo.EvAcc'

    --Update the start date for the month after
    SET @start_date = CONVERT(VARCHAR(8),DATEADD(MONTH,1,CONVERT(VARCHAR(8),@start_date,112)),112)

    --If the date is not the last date, it add a union
    IF @start_date < @end_date
    BEGIN
        SET @sql = @sql + ' UNION ALL '
    END
END

-- drop and create a view with new information
USE dbacces1
IF EXISTS (SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = 'viewEvAcc')DROP VIEW dbo.viewEvAcc; 
EXEC(@sql)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:42:33+00:00Added an answer on June 15, 2026 at 8:42 pm

    If the view changes periodically (i.e. every day), you could script a job to drop and recreate (alter) the view as needed. It could be part of the job that creates and/or populates that new table.

    If the empty tables are created ahead of time, look into creating a partitioned view to union them all. You can then put your query with a join on top of that and add the date constraint to that query’s where clause.

    Partitioned Views

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table structure on a SQL Server 2008 R2 database: CREATE TABLE
I have a table like this in my database (SQL Server 2008) ID Type
I have this question in which I have a SQL Server Compact Edition database
In SQL Server, I have a database abc . In this database I have
I have a database on a SQL Server 2000 server. This database has a
In my SQL Server database schema I have a data table with a date
I have an audit table in my SQL Server 2008 database which contains the
In a SQL SERVER 2005 database, please suppose we have a table called RUNNING_COLA_IS_AFRICA.
I have an MS SQL Server with a database for an E-commerce storefront. This
I am using SQL Server 2008, and in my database I have a table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.