Solved

ActiveX function in the DTS

Posted on 2009-07-10
9
434 Views
Last Modified: 2012-05-07
Hello Expert,
I am trying to create a DTS to generate a new table based on a monthly historical table. Since the table name changes every month, therefore I am trying to create a ActiveX function to pick up the latest monthly table.  For example, for June, the table name will be testing_6_2009, and for May, it will be testing_5_2009 and so on.

Here is the function I am trying to use:
'**********************************************************************
' Visual Basic ActiveX Script
'************************************************************************
Function Main()
  Dim strSQL
     strSQL = "select * into Testing from 'Testing_"& month(now()-20) &"_"&  year(now()-7) & "';"
    DTSGlobalVariables(TestingSQL").Value = strSQL
   Main = DTSTaskExecResult_Success
   End Function

This is the steps I did so far:
Step1: I used a Execute SQL task to generate a new table called Testing
Step2: I used a ActiveX Script Task to hold the above function
Step3: I used a Dynamic Properties Task to pass the variable
Step4: I used a Execute SQL Task to hold the following SQL to generate the new "Testing" table
           select * into testing  from 'Testing_?_?';

The first 3 steps run successfully, when I execute the fourth step, it gave me the following error message:
Incorrect syntax near Testing_6_2009

Please suggest what I did wrong. Thanks for your help. Have a great day.

above

Open in new window

question.doc
0
Comment
Question by:salesprod
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 26

Accepted Solution

by:
Zberteoc earned 500 total points
ID: 24824287
You don't need ActiveX for this you can use dynamic SQL and will be faster and easier to maintain. You can either put the code below in a stored procedure or directly in an Execute SQL task in the DTS package:

declare
	@sql varchar(8000)
select
	@sql='select * into Testing from [Testing_' + cast(month(getdate()-20) as varchar) + '_' + cast(year(getdate()-7) as varchar) +']'
	
print @sql	-- for testing only
EXEC (@sql)

Open in new window

0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 24824333
If you still have to use the ActiveX code remove the single quotes around the table name and change the month and year to strings:

Probably would work without changing to strings but with them, will work for sure.
     strSQL = "select * into Testing from Testing_"& Cstr(month(now()-20)) &"_"&  Cstr(year(now()-7)) & ";"

Open in new window

0
 

Author Comment

by:salesprod
ID: 24824363
Thanks Zberteoc, Let me try it...
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

Author Comment

by:salesprod
ID: 24824432
Awesome. It works. Thanks Zberteoc.
0
 

Author Comment

by:salesprod
ID: 24824610
Hi Zberteoc,
If i want to create the new testing table to a different database called "Sales" and I try to use the script you gave to me as follows. Why it does not work?

declare
      @sql varchar(8000)
select
      @sql='select * into Testing from [Sales.dbo.Testing_' + cast(month(getdate()-20) as varchar) + '_' + cast(year(getdate()-7) as varchar) +']'
      
print @sql      -- for testing only
EXEC (@sql)
0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 24827783
Because the Sales.dbo.Testing_...(date here)  doesn't exist yet. You'll have to create it first or if you want to use the same table from the initial db then you'll have to qualify the table with the database:


declare
        @sql varchar(8000)
select
        @sql='select * into Testing from [DATABASE_NAME]..[Testing_' + cast(month(getdate()-20) as varchar) + '_' + cast(year(getdate()-7) as varchar) +']'
        
print @sql      -- for testing only
EXEC (@sql)

Open in new window

0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 24827811
Oh, sorry, now I see the problem, is the way you use the brakets, []. The brackets has to wrap individually the database name then the owner(schema) and then the object, table in this case. The [] are only necessary if the names are the same with key words like table, month etc or if they have spaces inside:

select * from [Month] or select * from [Table] (if the table's name is really Table)

or

select * from [Sales Table]

otherwise they are not necessary.

So in order the cod to work you need:


declare
      @sql varchar(8000)
select
      @sql='select * into Testing from [Sales].[dbo[.[Testing_' + cast(month(getdate()-20) as varchar) + '_' + cast(year(getdate()-7) as varchar) +']'
      
print @sql      -- for testing only
EXEC (@sql)

Open in new window

0
 
LVL 26

Expert Comment

by:Zberteoc
ID: 24827816
sorry:
declare
      @sql varchar(8000)
select
      @sql='select * into Testing from [Sales].[dbo].[Testing_' + cast(month(getdate()-20) as varchar) + '_' + cast(year(getdate()-7) as varchar) +']'
      
print @sql      -- for testing only
EXEC (@sql)

Open in new window

0
 

Author Comment

by:salesprod
ID: 24831826
got it. Thanks Zberteoc.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
PERFORMANCE OF SQL QUERY 13 77
Stored procedure 4 36
Why is the output of this function is like this? 4 44
ISDATE() not working properly on my table? Any suggestions. 7 44
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question