Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Returning schema from dynamic aggrigate query

$
0
0

I am creating a database to run aggregate ETLs for end customers.  Customers will put data into a table which will dynamically run the sql and put it into an end table.  The end table name is also included in the meta-data table with the query, but I need to find a way to dynamically create the table if it doesn't exist.

Because the source table exists on a remote server and I am not permitted to do linked server queries, I need a way to get the schema of my returned queries.

An example of the type of query I am talking about is:

CREATE TABLE Sales ( BusinessDate datetime,
          ColPK INT IDENTITY(1,1),
          Sales MONEY,
          SourceType INT,
          Region VARCHAR(50)) 

DECLARE @Rows INT
SELECT @Rows = 50
WHILE @Rows >1
BEGIN
         
INSERT INTO Sales (BusinessDate,Sales,SourceType,Region)

SELECT BusinessDate = DATEADD(dd,CAST( RAND( (DATEPART(ms, GETDATE()) * 100000 )
           + (DATEPART(ss, GETDATE()) * 1000 )
           + DATEPART(mcs, GETDATE()) ) *-1000 AS INT),GETDATE())

,Sales = CAST( RAND( (DATEPART(mm, GETDATE()) * 100000 )
           + (DATEPART(ss, GETDATE()) * 1000 )
           + DATEPART(ms, GETDATE()) ) * 10000 AS MONEY)
,SourceType = CAST( RAND( (DATEPART(ss, GETDATE()) * 1300 )
           + DATEPART(ms, GETDATE()) ) * 10 AS INT)
,Region = CASE (CAST( RAND( (DATEPART(ms, GETDATE()) * 123400 )
           + (DATEPART(ss, GETDATE()) * 1200 )
           + DATEPART(mcs, GETDATE()) ) * 5 AS INT))
     WHEN 0 THEN 'North America'
     WHEN 1 THEN 'Europe'
     WHEN 2 THEN 'Africa'
     WHEN 3 THEN 'Asia'
     WHEN 4 THEN 'South America'
 END

SELECT @Rows = @Rows-1

END

--Query I need schema for is:

SELECT TotalSales = SUM(Sales)
 ,Region
 ,Yr = DATEPART(yyyy,BusinessDate)
 ,Mo = DATEPART(mm,BusinessDate)
FROM Sales
GROUP BY Region
 ,DATEPART(yyyy,BusinessDate)
 ,DATEPART(mm,BusinessDate)

Thank you for any guidance in this,

David


Don't just dream it, become it!


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>