|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: 2 days ago @ 1:49 PM
Points: 145,
Visits: 701
|
|
I am writing a stored procedure with dynamic query to find the record counts from two tables. The table names will be passed as parameters from SSIS -hence the table names will be dynamic. I need to capture the record count into a variable and insert them into a table as well as also pass them back to SSIS for comparison. I can't really get to work as i am not a dynamic sql expert. Any insights will be appreciated. thanx.
CREATE PROCEDURE [dbo].[spDmValidationCheck]( @DVTableName NVARCHAR(101) ,@DataMartTableName NVARCHAR(101) ,@DVTableRecordCount BIGINT OUTPUT ,@DataMartTableRecordCount BIGINT OUTPUT )
AS BEGIN DECLARE @CurrentDateTime DATETIME=GETDATE() DECLARE @SQL NVARCHAR(MAX)='SELECT COUNT (*) FROM ' DECLARE @DmSQL NVARCHAR(MAX)='' DECLARE @DvSQL NVARCHAR(MAX)=''
SET @DmSQL=@SQL+@DataMartTableName EXEC @DVTableRecordCount = sp_executesql @DmSQL OUTPUT ---?? INSERT INTO Test.dbo.DmValidation(DVTableName, DataMartTableName,DVTableRecordCount,DataMartTableRecordCount ,LogDateTime, RemarksAction) VALUES (@DVTableName,@DataMartTableName,@DVTableRecordCount,@DataMartTableRecordCount,@CurrentDateTime,NULL)
SELECT ISNULL(@DataMartTableRecordCount,0)AS DataMartTableRecordCount,ISNULL(@DVTableRecordCount,0) AS DVTableRecordCount END
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 1:02 PM
Points: 477,
Visits: 3,652
|
|
To make what you have work, take a look a this:
CREATE PROCEDURE [dbo].[spDmValidationCheck]( @DVTableName NVARCHAR(101) ,@DataMartTableName NVARCHAR(101) ,@DVTableRecordCount BIGINT OUTPUT ,@DataMartTableRecordCount BIGINT OUTPUT ) AS BEGIN
DECLARE @CurrentDateTime DATETIME=GETDATE() DECLARE @SQL1 NVARCHAR(MAX)=N'SELECT @DVTableRecordCount = COUNT(*) FROM ' DECLARE @SQL2 NVARCHAR(MAX)=N'SELECT @DataMartTableRecordCount = COUNT(*) FROM ' DECLARE @DmSQL NVARCHAR(MAX)='' DECLARE @DvSQL NVARCHAR(MAX)=''
SET @DmSQL=@SQL1+@DVTableName EXEC sp_executesql @DmSQL, N'@DVTableRecordCount BIGINT OUTPUT', @DVTableRecordCount OUTPUT
SET @DmSQL=@SQL2+@DataMartTableName EXEC sp_executesql @DmSQL, N'@DataMartTableRecordCount BIGINT OUTPUT', @DataMartTableRecordCount OUTPUT
INSERT INTO DmValidation(DVTableName, DataMartTableName, DVTableRecordCount, DataMartTableRecordCount, LogDateTime, RemarksAction) VALUES (@DVTableName,@DataMartTableName,@DVTableRecordCount,@DataMartTableRecordCount,@CurrentDateTime,NULL)
SELECT ISNULL(@DataMartTableRecordCount,0)AS DataMartTableRecordCount,ISNULL(@DVTableRecordCount,0) AS DVTableRecordCount END
______________________________________________________________________________ How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 1:02 PM
Points: 477,
Visits: 3,652
|
|
Of course, I'd do this task like below. Be warned though, that I don't have to worry about sql injection in my closed environment.
CREATE PROCEDURE [dbo].[spDmValidationCheckTODD]( @DVTableName NVARCHAR(101) ,@DataMartTableName NVARCHAR(101) ,@debug bit = 0) AS DECLARE @SQL VARCHAR(MAX) SET @SQL = ' INSERT INTO DmValidation(DVTableName, DataMartTableName, DVTableRecordCount, DataMartTableRecordCount, LogDateTime, RemarksAction) SELECT ' + '''' + @DVTableName + '''' + ', ' + '''' + @DataMartTableName + '''' + ', (SELECT COUNT(*) FROM ' + @DVTableName + '), (SELECT COUNT(*) FROM ' + @DataMartTableName + '), GETDATE(), '''' '
IF @debug = 1 PRINT @SQL EXEC(@SQL)
______________________________________________________________________________ How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Today @ 5:54 PM
Points: 32,907,
Visits: 26,796
|
|
toddasd (12/18/2012) Of course, I'd do this task like below. Be warned though, that I don't have to worry about sql injection in my closed environment.
Famous last words. 
You should always be prepared for SQL Injection because 1) You never know when your "closed environment" is going to be opened up, 2) you never know just how clever hackers are at getting into supposed "closed environments", and 3) a closed environment will not protect you from someone on the inside with a score to settle.
--Jeff Moden "RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: 2 days ago @ 1:02 PM
Points: 477,
Visits: 3,652
|
|
Jeff Moden (12/18/2012)
toddasd (12/18/2012) Of course, I'd do this task like below. Be warned though, that I don't have to worry about sql injection in my closed environment.Famous last words.  You should always be prepared for SQL Injection because 1) You never know when your "closed environment" is going to be opened up, 2) you never know just how clever hackers are at getting into supposed "closed environments", and 3) a closed environment will not protect you from someone on the inside with a score to settle.
True on all counts. I regretted posting that a minute after clicking submit. Trouble is my entire system is built like this. I guess it's time to go retrofitting.
______________________________________________________________________________ How I want a drink, alcoholic of course, after the heavy lectures involving quantum mechanics.
|
|
|
|