Thursday, October 18, 2012

How to find out missing data in columns

Sometimes, I need to compare two columns to find out missing value

such as column a contains list of orderid from one script while column b contains list of orders from another script

Since two scripts are running on different env, the only way to compare is to copy and paste to excel.

Now if list contains thousands of orders, it is inhumane to go through each row

Here is the solution:  http://en.kioskea.net/forum/affich-456715-find-missing-data-in-2-columns-excel

use COUNTIF function in excel

Suppose A column contains less orders than E

You want to find out how many are missing in column

=IF(COUNTIF(A$2:A$3730,E2521)=0,E2521,"")

Remember to use $

What it does it to take each value in E column and go through the whole A column, if there is no match, display that value from Column E

 

Neat !

Tuesday, October 16, 2012

How to create a temp table using dynamic sql

When using dynamic sql to create a temp table, you need to create the temp table first, then alter columns in the dynamic sql

see this blog below:

 

Temporary Tables and Dynamic SQL

When programming in SQL on occasion there is a need to generate temporaty tables inside a sproc without knowing the colums or type when the sproc is written. One such situation would be when you want to pivot a table. You can create a cursor over a column result set and create a new column for each value.
When you try to create temporary tables with dynamic SQL you will run into a scoping problem.
for example:
DECLARE @SQL nvarchar(4000)
SELECT @SQL = 'CREATE TABLE #Temp (col1 int)'
EXEC (@SQL)
SELECT * FROM #Temp

This will cause an error:

Msg 208, Level 16, State 0, Line 4
Invalid object name '#Temp'.

The problem here is the scope of the session. When we execute dynamic sql via EXEC or sp_executesql a new scope is created for a child session. Any objects created in that session are dropped as soon as the session is closed.

One solution I have found for this problem is creating the table in the "parent" scope and then just using dynamic sql to modify the table. For this to work a table is created with a minimum set of colums. And then we use the ALTER TABLE statement with dynamic SQL. The Child session has access to the objects created in the parent session so the table can be modified with dynamic sql:

DECLARE @SQL NVARCHAR(4000)
CREATE TABLE #Temp ( id int null)
SELECT @SQL = 'ALTER #Temp ADD Col1 int null'
EXEC (@SQL)
SELECT * FROM #Temp
DROP TABLE #Temp

This table is visible and both columns will show up.

Tuesday, October 9, 2012

How to open Sql files in TFS using SSMS instead of default vs2010 window

 

to be filled…

How to execute SSRS report from Sql Server job agent

 

Sometimes, there are needs to execute SSRS directly from a sql server job or inside a SSIS package.

If it is from a SSIS package, you need to use script component and use the web service exposed by SSRS

If you need to execute it through a Sql server job agent, below is the step

 

1.  Create a one time subscription for that report.

2. Find out the Subscription ID for that report

SELECT s.SubscriptionID

FROM dbo.Catalog AS c

INNER JOIN dbo.ReportSchedule AS s

             ON   c.ItemID = s.ReportID

WHERE

      c.Name = ‘your report name’

3. Execute dbo.AddEvent proc on ReportServer

 

EXEC ReportServer.dbo.AddEvent @EventType = 'TimedSubscription', @EventData = @SubscriptionID

 

This SSRS rpt will be executed  by this job.