Ashwani 的个人资料Ashwani Roy's BI Blog照片日志列表更多 ![]() | 帮助 |
|
|
7月23日 SQL Server 2008 – Merge , Grouping Sets , table Valued Parameters and some other stuff !!Some time back I took a small session for a group of SQL Server users about some cool stuff in SQL Server 2008 from the T SQL point of view. Some of the students wanted to have access to the scripts , so here they are MERGE STATEMENT and OUTPUT CLAUSE drop table Source go CREATE TABLE Source (id int,val varchar(56)); CREATE TABLE Destination (id int,val varchar(56)); GO INSERT Source VALUES (1,'A'); INSERT Destination VALUES (1,'q');--this will be updated to A GO MERGE Destination D -- target table USING Source S -- source table ON S.ID = D.ID WHEN MATCHED THEN UPDATE set D.val = S.val WHEN NOT MATCHED THEN -- insert a row if the stock is newly acquired INSERT VALUES (S.ID, S.VAL) -- output details of INSERT/UPDATE/DELETE operations -- made on the target table OUTPUT $action, inserted.*, deleted.*; ---this is not required in Merge statement I have kept it here just to demostrate that from SQL 2005 onwards $action id val id val--- I can see what is Inserted and what is Deleted */ SELECT * FROM Destination;--See the result GO
GROUPING SETS GROUPING SETS are a new feature of SQL Server 2008. Using them will allow multiple groupings to be returned in one record set. We will grouping on City and StateProvice in the same query use AdventureWorksLT --useing GROUPING SETS in SQL 2008 TABLE VALUED PARAMTERS In this example I will pass a table valued parameter in the stored procedure --USING Table valued input Parameters ALTER PROCEDURE myProc (@tvp myTableType READONLY) AS declare @table TABLE (id int) UPDATE Inventory SET
NEW DATE TIME DATA TYPES -- IN SQL 2008 INSTANCE DECLARE @DATE DATE , @TIME TIME DECLARE @DATETIMEOFFSET datetimeoffset -------------------------------------------------------------------------------------------------------------------------- Thats all for now!! I though it will be easier to get the scripts from here rather than download from my sky drive. It is not a very extensive list , it just covers the 1 hours session which I took for beginner sql users. A quick Look at CDC –(Change data Capture)
What is CDC :- Change data capture (CDC) is a set of software design pattern used to determine (and track) the data that has changed so that action can be taken using the changed data. Also, Change data capture (CDC) is an approach to data integration that is based on the identification, capture and delivery of the changes made to enterprise data sources. It is a very common requirement for a data warehouse load package is to determine what has changed in the source systems and load this data into the warehouse. All I wanted to give was a script that you can use along with adventure works Database to see what are basic stuff in CDC and how it works. off course you can use it in your Data warehouse SSIS packages. --------------------------------------------------------------- USE AdventureWorks --Enable Change Tracking on a table-- select * from HumanResources.Employee where Title = 'Production Technician - WC60' --Get the Data -- select * from cdc.fn_cdc_get_all_changes_HumanResources_Employee declare @minLSN varbinary(max),@maxLsn varbinary(max) select @minLSN = sys.fn_cdc_get_min_lsn('HumanResources_Employee') select * from cdc.fn_cdc_get_all_changes_HumanResources_Employee --Disable CDC as I dont want this running on my Laptop-- */ --exec sp_configure ----------------------------------------------------------------------------------------------------- 11月13日 Get first and last Day of the Current WeekIt might not the be most trivial Stuff but I saw some posts on microsoft forums for this . So I thought I should blog it select Getdate() -- this will give you todays date SELECT DATEADD(wk, DATEDIFF(wk, 6,Getdate() ), 6) --First day of the week On forums http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=4119871&SiteID=1 |
|
|