Solved

ActiveX function in the DTS

Posted on 2009-07-10
9
432 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
  • 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

803 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