tag:blogger.com,1999:blog-358831767242667052024-03-21T12:29:12.472-07:00Ashish AgarwalAshish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.comBlogger38125tag:blogger.com,1999:blog-35883176724266705.post-30341238427894116062018-09-26T23:11:00.000-07:002018-09-26T23:11:22.619-07:00SQL Function to replace special characters from a string<div dir="ltr" style="text-align: left;" trbidi="on">
CREATE Function [dbo].[replaceSpecialCharacters] (@str varchar(256)) returns varchar(256)<br />
with schemabinding<br />
begin<br />
<span style="white-space: pre;"> </span>SET @str = LTRIM(RTRIM(@str))<br />
DECLARE @expres VARCHAR(50) = '%[~,@,#,$,%,&,*,{,},(,),.,!,:,?,^,+,=,;,-,/,\]%'<br />
WHILE PATINDEX( @expres, @str ) > 0<br />
BEGIN<br />
<span style="white-space: pre;"> </span> SET @str = REPLACE(@str, SUBSTRING( @str, PATINDEX( @expres, @str ), 1 ),'_')<br />
<span style="white-space: pre;"> </span> END<br />
<br />
<span style="white-space: pre;"> </span>SET @str = Replace(@str,' ','||')<br />
<span style="white-space: pre;"> </span>SET @str = Replace(@str,'’','')<br />
<span style="white-space: pre;"> </span>SET @str = Replace(@str,'_','')<br />
<span style="white-space: pre;"> </span>SET @str = Replace(@str,'||','_')<br />
<span style="white-space: pre;"> </span>SET @str = Replace(@str,'__','_')<br />
<span style="white-space: pre;"> </span>SET @str = Replace(@str,'__','_')<br />
<span style="white-space: pre;"> </span>SET @str = LTRIM(RTRIM(@str))<br />
<br />
return @str<br />
END<br />
GO</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-65329131575176863732018-09-17T04:47:00.003-07:002018-09-17T04:47:52.799-07:00Excel Based Reporting<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif; font-size: x-small;">Hello Everyone, so today my focus is on to give insights about how can we deal with scenarios where the client wants us to help them with Excel based reporting.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;">We will be doing this using SSIS as we will need automation of the report on a daily basis.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;">Technology Items used -</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;">1. SSIS</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;">2. Excel files (format files)</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;">3. Macro codes</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;">4. Conditional formatting.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;">5. SSIS</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;">6. C#.Net / VB.Net Script Components</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><u style="font-weight: bold;">Step-1:</u> - We will be writing a few macro codes to do the same as we will need the numeric columns to be in the numeric while we insert the data from SSIS it will automatically convert it to Text.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><b style="text-decoration-line: underline;">Macro-1</b> - Code to Convert Text to Integer.</span><br />
<br />
<span style="color: blue;">Sub </span>Function_Name()<br />
sheetlist = Array("Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6", "Sheet7")<br />
<span style="color: blue;">For </span>i = <span style="color: blue;">LBound</span>(sheetlist) <span style="color: blue;">To UBound</span>(sheetlist)<br />
Worksheets(sheetlist(i)).Activate<br />
<span style="color: #38761d;">' Your Code Goes Here</span><br />
Range("F:F").Select <span style="color: #274e13;">'specify the range which suits your purpose</span><br />
<span style="color: blue;">With </span>Selection<br />
Selection.NumberFormat = "#,##0.00"<br />
.Value = .Value<br />
<span style="color: blue;">End With</span><br />
<span style="color: blue;"><br /></span>
<span style="color: blue;">Next</span><br />
<span style="color: blue;">End Sub</span><br />
<span style="color: blue;"><br /></span>
<b style="font-family: Verdana, sans-serif; font-size: small; text-decoration-line: underline;">Macro-2</b><span style="font-family: Verdana, sans-serif; font-size: x-small;"> - Code to Activate the first sheet in the excel work book.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><span style="color: blue;">Sub </span>ActivateFirstWorksheet()</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><span style="color: blue;">Dim </span>ws <span style="color: blue;">As </span>Worksheet</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">For Each</span> ws <span style="color: blue;">In </span>ActiveWorkbook.Sheets</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> ws.Activate</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> ws.[a1].Select</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">Next </span>ws</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> ActiveWorkbook.Worksheets(1).Activate</span><br />
<br />
<span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">End Sub</span><br />
<span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<b style="font-family: Verdana, sans-serif; font-size: small; text-decoration-line: underline;">Macro-3</b><span style="font-family: Verdana, sans-serif; font-size: x-small;"> - Code to Convert the Data Table in to Pivot Table.</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><span style="color: blue;">Sub </span>createPivotTableExistingSheet()</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'Source: https://powerspreadsheets.com/</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'For further information: https://powerspreadsheets.com/vba-create-pivot-table/</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'declare variables to hold row and column numbers that define source data cell range</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myFirstRow <span style="color: blue;">As Long</span></span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myLastRow </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">As </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Long</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myFirstColumn </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">As </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Long</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myLastColumn </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">As </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Long</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'declare variables to hold source and destination cell range address</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">Dim </span>mySourceData </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">As </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">String</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myDestinationRange </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">As </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">String</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'declare object variables to hold references to source and destination worksheets, and new Pivot Table</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">mySourceWorksheet <span style="color: cyan;">As </span>Worksheet</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myDestinationWorksheet <span style="color: blue;">As </span>Worksheet</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">Dim </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">myPivotTable As PivotTable</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'identify source and destination worksheets</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">With </span>ThisWorkbook</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">Set </span>mySourceWorksheet = .Worksheets("SheetName")</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">Set </span>myDestinationWorksheet = .Worksheets("PivotTable")</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">End </span></span><span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">With</span><span style="font-family: Verdana, sans-serif; font-size: x-small;"> </span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'obtain address of destination cell range</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> myDestinationRange = myDestinationWorksheet.Range("A5").Address(ReferenceStyle:=xlR1C1)</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'identify row and column numbers that define source data cell range</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> myFirstRow = 1</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> myLastRow = 20005</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> myFirstColumn = 1</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> myLastColumn = 17</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'obtain address of source data cell range</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">With</span> mySourceWorksheet.Cells</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> mySourceData = .Range(.Cells(myFirstRow, myFirstColumn), .Cells(myLastRow, myLastColumn)).Address(ReferenceStyle:=xlR1C1)</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">End With</span></span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'create Pivot Table cache and create Pivot Table report based on that cache</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">Set </span>myPivotTable = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=mySourceWorksheet.Name & "!" & mySourceData).CreatePivotTable(TableDestination:=myDestinationWorksheet.Name & "!" & myDestinationRange, TableName:="PivotTableExistingSheet")</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> 'add, organize and format Pivot Table fields</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">With</span> myPivotTable</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> .PivotFields("Instrument").Orientation = xlRowField</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> .PivotFields("Trade Type").Orientation = xlColumnField</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;">With </span>.PivotFields("Nominal")</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> .Orientation = xlDataField</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> .Position = 1</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> .Function = xlSum</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> .NumberFormat = "#,##0.00"</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"> <span style="color: blue;"> End With</span></span><br />
<span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;"> End With</span><br />
<span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<br />
<span style="color: blue; font-family: Verdana, sans-serif; font-size: x-small;">End Sub</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><b><u>Calling the Macros using the SSIS Script Task - </u></b></span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><b><u><br /></u></b></span>
<span style="font-family: Verdana, sans-serif; font-size: x-small;">Once this all is done We need to get these macros called from SSIS using the script task and then save the workbook as shown below -</span><br />
<span style="font-family: Verdana, sans-serif; font-size: x-small;"><br /></span>
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">#Region</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Imports"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Imports</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> System<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Imports</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> System.Data<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Imports</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> System.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Math</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Imports</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Microsoft.SqlServer.Dts.Runtime<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Imports</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Microsoft.Office.Interop.Excel<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">#End</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Region</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'ScriptMain
is the entry point class of the script.<span style="mso-spacerun: yes;">
</span>Do not change the name, attributes,</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'or
parent of this class.</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><Microsoft.SqlServer.Dts.Tasks.ScriptTask.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">SSISScriptTaskEntryPointAttribute</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">()><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><System.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">CLSCompliantAttribute</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">(</span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">False</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Partial</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Class</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ScriptMain</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Inherits</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Microsoft.SqlServer.Dts.Tasks.ScriptTask.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">VSTARTScriptObjectModelBase</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Public</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Sub</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Main()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dim</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> excel </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">As</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">New</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Microsoft.Office.Interop.Excel.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Application</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Dim</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> wb </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">As</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> Microsoft.Office.Interop.Excel.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Workbook</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>wb = excel.Workbooks.Open(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Macro Based Excel file Path.xlsm"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>excel.Run(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Macro_1"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>excel.Run(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Macro_2"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>excel.Run(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Macro_3"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>excel.Run(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Macro_4"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>excel.Run(</span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"Macro_5"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'wb.RefreshAll()</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>wb.Save()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>wb.Close()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>excel.Quit()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>Dts.TaskResult = </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ScriptResults</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Success<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">End</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Sub</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">#Region</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #a31515; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">"ScriptResults
declaration"</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'This
enum provides a convenient shorthand within the scope of this class for setting
the</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'result
of the script.</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: green; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">'This
code was generated automatically.</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Enum</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">ScriptResults</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>Success =
Microsoft.SqlServer.Dts.Runtime.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DTSExecResult</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Success<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span>Failure =
Microsoft.SqlServer.Dts.Runtime.</span><span style="background: white; color: #2b91af; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">DTSExecResult</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">.Failure<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">End</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Enum</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">#End</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"> </span><span style="background: white; color: grey; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;">Region</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; mso-highlight: white;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">End</span><span style="background: white; color: black; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;"> </span><span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;">Class</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="background: white; color: blue; font-family: Consolas; font-size: 9.5pt; line-height: 115%; mso-highlight: white;"><br /></span></div>
<div class="MsoNormal">
<span style="font-family: Verdana, sans-serif; font-size: x-small;">Hope you liked this Article, Happy Learning !</span></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-51531447630377190242018-03-07T22:42:00.000-08:002018-03-07T22:42:27.793-08:00Sync SSAS Cube From One Environment to Another<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal">
</div>
<div class="MsoNormal">
<b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">Please Execute the Below Mentioned Script in Either the SQL Agent Job or XMLA Script Window or in the SSIS Package using the "Analysis Services Execute DDL Task" - </span></b></div>
<div class="MsoNormal">
<b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;"><br /></span></b></div>
<div class="MsoNormal">
<b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;"><</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">Synchronize </span></b><b><span style="background: white; color: red; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">xmlns:xsi</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">=</span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">"</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">https://www.w3.org/2001/XMLSchema-instance</span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">"</span></b><b><span style="background: white; color: red; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">xmlns:xsd</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">=</span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">"</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">https://www.w3.org/2001/XMLSchema</span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">"</span></b><b><span style="background: white; color: red; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">xmlns</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">=</span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">"</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">https://schemas.microsoft.com/analysisservices/2003/engine</span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">"</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
<</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">Source</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
<</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">ConnectionString</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">></span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">Provider=MSOLAP.6;Data
Source=SOURCESERVER; Integrated Security=SSPI;Initial Catalog=SourceCubeName</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;"></</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">ConnectionString</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
<</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">Object</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
<</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">DatabaseID</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">></span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">SourceCubeName</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;"> </</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">DatabaseID</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
</</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">Object</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
</</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">Source</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
<</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">SynchronizeSecurity</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">></span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">CopyAll</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;"></</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">SynchronizeSecurity</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
<</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">ApplyCompression</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">></span></b><b><span style="background: white; font-family: "Segoe UI", sans-serif; font-size: 9pt; line-height: 115%;">true</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;"></</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">ApplyCompression</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">><br />
</</span></b><b><span style="background: white; color: #a31515; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">Synchronize</span></b><b><span style="background: white; color: blue; font-family: "Segoe UI",sans-serif; font-size: 9.0pt; line-height: 115%;">></span></b><o:p></o:p></div>
<br />
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-79694249583101094572018-02-28T05:05:00.000-08:002018-02-28T05:06:50.954-08:00XML Data Load Using SSIS Script task C#.Net<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">XmlDataDocument</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> xmldoc = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">XmlDataDocument</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">XmlNodeList</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> xmlnode,xmlnode1;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> i = 0,j=0;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> str = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">null</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">string</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> FilePath = Dts.Variables[</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"User::File_Name"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">].Value.ToString();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">int</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> nodecount = 0;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FileStream</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> fs = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FileStream</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">(@FilePath, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FileMode</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">.Open, </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">FileAccess</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">.Read);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">xmldoc.Load(fs);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">xmlnode =
xmldoc.GetElementsByTagName(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"TagName"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">xmlnode1 = xmldoc.GetElementsByTagName(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"AnotherTagName"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">);</span><span style="background-color: white; font-family: "consolas"; font-size: 9.5pt;"> </span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">nodecount = xmlnode.Count;</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<br /></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: 0.0001pt; text-align: left;">
<span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">for</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> (i = 0; i <= xmlnode.Count - 1; i++)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p> </o:p></span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">for</span><span style="background: white; font-family: "consolas"; font-size: 9.5pt;"> (j = 8; j <= xmlnode[i].ChildNodes.Count - 2; j++)</span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>str =
xmlnode[i].ChildNodes.Item(0).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(1).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(2).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(3).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(4).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(5).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(6).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+ xmlnode[i].ChildNodes.Item(7).InnerText.Trim()
+ </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(j).ChildNodes.Item(0).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(j).ChildNodes.Item(1).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(j).ChildNodes.Item(2).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(j).ChildNodes.Item(3).InnerText.Trim() + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"|"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>+
xmlnode[i].ChildNodes.Item(j).ChildNodes.Item(4).InnerText.Trim()<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>;<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//MessageBox.Show(str);</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> t</span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">ry</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SqlConnection</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> conn = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SqlConnection</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Data source=YourServerName;
Database=YourDBName;Integrated Security=SSPI"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>conn.Open();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SqlCommand</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> cmd = </span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">new</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> </span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">SqlCommand</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">(</span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"Insert into YourTableName select '"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> + str + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"','"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> + FilePath + </span><span style="background: white; color: #a31515; font-family: "consolas"; font-size: 9.5pt;">"'"</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">, conn);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>cmd.ExecuteNonQuery();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: green; font-family: "consolas"; font-size: 9.5pt;">//MessageBox.Show("Inserted
Data Successfully !!");</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>conn.Close();<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt;">catch</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> (</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">Exception</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"> e)<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>{<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt;">MessageBox</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;">.Show(e.Message);<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span>}<o:p></o:p></span></div>
<div class="MsoNormal" style="line-height: normal; margin-bottom: .0001pt; margin-bottom: 0in; mso-layout-grid-align: none; text-autospace: none;">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt;"><o:p> </o:p></span><span style="background-color: white; font-family: "consolas"; font-size: 9.5pt;">fs.Close();</span></div>
<div class="MsoNormal">
<span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">Dts.TaskResult = (</span><span style="background: white; color: blue; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">int</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">)</span><span style="background: white; color: #2b91af; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">ScriptResults</span><span style="background: white; color: black; font-family: "consolas"; font-size: 9.5pt; line-height: 115%;">.Success;</span><o:p></o:p></div>
<br /></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-34673559366033614322016-07-06T04:01:00.004-07:002016-07-06T04:01:46.138-07:00List of Columns of a Table in CSV Format<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@TABLE_NAME <span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span>128<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@SCHEMA_NAME <span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span>128<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">-----------------------------------------------------------------------<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">-- *** User
Customisation <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">-- Set up the
name of the table here :<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@TABLE_NAME <span style="color: grey;">=</span> <span style="color: red;">'Address'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">-- Set up the
name of the schema here, or just leave set to 'dbo' :<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@SCHEMA_NAME <span style="color: grey;">=</span> <span style="color: red;">'dbo'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">-----------------------------------------------------------------------<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@vvc_ColumnName <span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span>128<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@vvc_ColumnList <span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span><span style="color: magenta;">MAX</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">IF</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@SCHEMA_NAME <span style="color: grey;">=</span><span style="color: red;">''<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">PRINT</span> <span style="color: red;">'Error : No schema defined!'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">RETURN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">IF</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">NOT</span> <span style="color: grey;">EXISTS</span><span style="color: blue;"> </span><span style="color: grey;">(</span><span style="color: blue;">SELECT</span> <span style="color: grey;">*</span> <span style="color: blue;">FROM</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">tables</span> T <span style="color: grey;">JOIN</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">schemas</span> S<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ON</span> T<span style="color: grey;">.</span><span style="color: magenta;">schema_id</span><span style="color: grey;">=</span>S<span style="color: grey;">.</span><span style="color: magenta;">schema_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">WHERE</span>
T<span style="color: grey;">.</span>Name<span style="color: grey;">=</span>@TABLE_NAME
<span style="color: grey;">AND</span> S<span style="color: grey;">.</span>name<span style="color: grey;">=</span>@SCHEMA_NAME<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">PRINT</span> <span style="color: red;">'Error : The table '''</span><span style="color: grey;">+</span>@TABLE_NAME<span style="color: grey;">+</span><span style="color: red;">''' in schema '''</span><span style="color: grey;">+<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @SCHEMA_NAME<span style="color: grey;">+</span><span style="color: red;">''' does not exist in this database!'</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">RETURN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
TableCursor <span style="color: blue;">CURSOR</span> <span style="color: blue;">FAST_FORWARD</span>
<span style="color: blue;">FOR<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">CASE</span> <span style="color: blue;">WHEN</span> <span style="color: magenta;">PATINDEX</span><span style="color: grey;">(</span><span style="color: red;">'% %'</span><span style="color: grey;">,</span>C<span style="color: grey;">.</span>name<span style="color: grey;">)</span> <span style="color: grey;">></span> 0 <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">THEN</span> <span style="color: red;">'['</span><span style="color: grey;">+</span> C<span style="color: grey;">.</span>name <span style="color: grey;">+</span><span style="color: red;">']'</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ELSE</span> C<span style="color: grey;">.</span>name <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">columns</span> C<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">JOIN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">tables</span> T<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ON</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> C<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">=</span> T<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">JOIN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">schemas</span> S<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ON</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> S<span style="color: grey;">.</span><span style="color: magenta;">schema_id</span> <span style="color: grey;">=</span> T<span style="color: grey;">.</span><span style="color: magenta;">schema_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> T<span style="color: grey;">.</span>name <span style="color: grey;">=</span>
@TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AND</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> S<span style="color: grey;">.</span>name <span style="color: grey;">=</span>
@SCHEMA_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BY</span> column_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@vvc_ColumnList<span style="color: grey;">=</span><span style="color: red;">''<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">OPEN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
TableCursor<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FETCH</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span> TableCursor
<span style="color: blue;">INTO</span> @vvc_ColumnName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHILE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">@@FETCH_STATUS</span><span style="color: grey;">=</span>0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@vvc_ColumnList <span style="color: grey;">=</span> @vvc_ColumnList <span style="color: grey;">+</span> @vvc_ColumnName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- get the details
of the next column<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FETCH</span> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span> TableCursor
<span style="color: blue;">INTO</span> @vvc_ColumnName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- add a comma if
we are not at the end of the row<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF</span> <span style="color: magenta;">@@FETCH_STATUS</span><span style="color: grey;">=</span>0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@vvc_ColumnList <span style="color: grey;">=</span> @vvc_ColumnList <span style="color: grey;">+</span> <span style="color: red;">','<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CLOSE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
TableCursor<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DEALLOCATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
TableCursor<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">PRINT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: red;">'Here is the comma separated list of column names :'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">PRINT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: red;">'--------------------------------------------------'</span> <o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">PRINT</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">
@vvc_ColumnList</span><o:p></o:p></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-78794653010884485152016-03-30T01:25:00.001-07:002016-03-30T01:25:26.194-07:00Get Directory Structure using Extended Stored Procedure xp_dirtree<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; color: #333333; font-family: verdana, tahoma, arial, sans-serif; font-size: 12px; line-height: 19.45px; margin-bottom: 0.7em; margin-top: 0.7em; padding: 0px; text-align: justify;">
<code style="margin: 0px; padding: 0px;"><span style="color: darkred; margin: 0px; padding: 0px;">Exec xp_dirtree </span><span style="color: red; margin: 0px; padding: 0px;">'C:\Windows'</span></code></div>
<div style="background-color: white; color: #333333; font-family: verdana, tahoma, arial, sans-serif; font-size: 12px; line-height: 19.45px; margin-bottom: 0.7em; margin-top: 0.7em; padding: 0px; text-align: justify;">
Execution of the above stored procedure will give following result. If you prefer you can insert the data in the temptable and use the same for further use.</div>
<div style="background-color: white; color: #333333; font-family: verdana, tahoma, arial, sans-serif; font-size: 12px; line-height: 19.45px; margin-bottom: 0.7em; margin-top: 0.7em; padding: 0px; text-align: justify;">
<img alt="" class="CToWUd a6T" height="263" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEgw_PDdznfyp5HJEg-tArX1SGu_gUOE41fTYlcPGYeU2vvU3oVxtDzO8D4TMA6Dep3HMpWz75lsim6k5ZCvrTJ0wNiTDFMvUsl5g14YWEuMBHIAWqiv0s1dqsHh28fpE9t66jOqmgZvqr8v=s0-d-e1-ft" style="background-image: url("https://blogger.googleusercontent.com/img/proxy/AVvXsEhOBB9MaGtVWnT7C565IsAut-D5TKpIzvtk2FM8f4pyIzgxYlAQ5e3YjK36KGQhrIQqZp4mBxNXLTa9e0VERf6VqYsZkLRmKBSagV57ouLtcmD0GmxatWEiElsatF0_jyD4uC8gzSALE5cg-ahaqdosZK75pEGfUKLpSgO2VLw=s0-d-e1-ft"); background-position: 100% 100%; background-repeat: no-repeat; border-left-color: rgb(238, 238, 238); border-left-width: 1px; border-style: solid none none solid; border-top-color: rgb(238, 238, 238); border-top-width: 1px; cursor: pointer; margin: 0px; max-width: 100%; min-height: auto; outline: 0px; padding: 4px 10px 10px 4px;" tabindex="0" width="420" /></div>
<div style="background-color: white; color: #333333; font-family: verdana, tahoma, arial, sans-serif; font-size: 12px; line-height: 19.45px; margin-bottom: 0.7em; margin-top: 0.7em; padding: 0px; text-align: justify;">
Here is the quick script which will insert the data into the temptable and retrieve from the same.</div>
<div style="background-color: white; color: #333333; font-family: verdana, tahoma, arial, sans-serif; font-size: 12px; line-height: 19.45px; margin-bottom: 0.7em; margin-top: 0.7em; padding: 0px;">
<code style="margin: 0px; padding: 0px;"><span style="color: blue; margin: 0px; padding: 0px;">CREATE TABLE </span><span style="color: #434343; margin: 0px; padding: 0px;">#TempTable </span><span style="color: grey; margin: 0px; padding: 0px;">(</span><span style="color: black; margin: 0px; padding: 0px;">Subdirectory<wbr></wbr> </span><span style="color: blue; margin: 0px; padding: 0px;">VARCHAR</span><span style="color: grey; margin: 0px; padding: 0px;">(</span><span style="color: black; margin: 0px; padding: 0px;">512</span><span style="color: grey; margin: 0px; padding: 0px;">), </span><span style="color: black; margin: 0px; padding: 0px;">Depth </span><span style="color: blue; margin: 0px; padding: 0px;">INT</span><span style="color: grey; margin: 0px; padding: 0px;">);</span><span style="color: blue; margin: 0px; padding: 0px;">INSERT INTO </span><span style="color: #434343; margin: 0px; padding: 0px;">#TempTable </span><span style="color: grey; margin: 0px; padding: 0px;">(</span><span style="color: black; margin: 0px; padding: 0px;">Subdirectory</span><span style="color: grey; margin: 0px; padding: 0px;">,<wbr></wbr> </span><span style="color: black; margin: 0px; padding: 0px;">Depth</span><span style="color: grey; margin: 0px; padding: 0px;">)</span><span style="color: blue; margin: 0px; padding: 0px;">EXEC </span><span style="color: darkred; margin: 0px; padding: 0px;">xp_dirtree </span><span style="color: red; margin: 0px; padding: 0px;">'C:\Windows'</span><span style="color: blue; margin: 0px; padding: 0px;">SELECT </span><span style="color: black; margin: 0px; padding: 0px;">Subdirectory</span><span style="color: grey; margin: 0px; padding: 0px;">, </span><span style="color: black; margin: 0px; padding: 0px;">Depth</span><span style="color: blue; margin: 0px; padding: 0px;">FROM </span><span style="color: #434343; margin: 0px; padding: 0px;">#TempTable</span><span style="color: grey; margin: 0px; padding: 0px;">;</span><span style="color: blue; margin: 0px; padding: 0px;">DROP TABLE </span><span style="color: #434343; margin: 0px; padding: 0px;">#TempTable</span><span style="color: grey; margin: 0px; padding: 0px;">;</span></code></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-4771294539011033072016-03-30T01:24:00.003-07:002016-03-30T01:24:34.057-07:00List Directory Files using T-SQL xp_cmdShell Stored Procedure<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
Here is a example code showing how to list directory files in a file folder using sql xp_cmdshell procedure.</div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
<br /></div>
<div style="background-color: white; border: 1px solid green; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
<code>CREATE TABLE dirList (<br /> id int identity(1,1),<br /> line nvarchar(1000)<br />)<br />GO;<br /><br />INSERT INTO dirList (line) EXEC xp_cmdshell 'dir C:\SQLDatabases'<br /><br />SELECT * FROM dirList;<br />WITH CTE AS (<br /> SELECT<br /> id,<br /> SUBSTRING(line,1,17) [date],<br /> SUBSTRING(line,18,19) sizeordir,<br /> SUBSTRING(line,37,100) name<br /> FROM dirList<br /> WHERE id > (<br /> SELECT MIN(id) FROM dirList WHERE line LIKE '%<DIR>%..%'<br /> ) AND id < (SELECT MAX(id) - 2 FROM dirList)<br />)<br />SELECT<br /> id,<br /> [date],<br /> isDirectory = CASE WHEN sizeordir LIKE '%<DIR>%' THEN 1 ELSE 0 END,<br /> isFile = CASE WHEN sizeordir LIKE '%<DIR>%' THEN 0 ELSE 1 END,<br /> name<br />FROM cte</code></div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
<br /></div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
The above t-sql script can be used to list files in directory using sql code.<br />The isDirectory column and isFile column can be used to distinguish files and folders within the target file folder.</div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
<br /></div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
<img alt="list-directory-files-using-sql-xp_cmdshell" class="CToWUd a6T" height="425px" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEhQvFcqU04LHARew8OptZH8NU5oV7PBstJ_l-bGpksjRGV5QTmkCYALHStEZQD5XLxD9lVcAvlMpkrhcyFt9757Ik8er4Eoq9ypn5pDOB9rW5YGMN6NREXywHQGycVuqvjQglvRyc_RJshiGv03KSyTKN3Oo3kk5fK4xMMncGR7uXr2HLhtwKikOZ-7T13h=s0-d-e1-ft" style="cursor: pointer; outline: 0px;" tabindex="0" title="list-directory-files-using-sql-xp_cmdshell" width="519px" /></div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
<br /></div>
<div style="background-color: white; color: #222222; font-family: Arial, Helvetica; font-size: 13px;">
By using recursive sql select statements, it is possible to list all files and all folders under the root c drive.<br />Again recursive SQL statement will enable to get the directory structure of the target file folder or disk drive given as parameter in the t-sql xp_cmdshell statement.</div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-35814998544036723462016-03-30T01:22:00.001-07:002016-03-30T01:22:15.677-07:00Additive, Semi-Additive and Non-Additive Measures<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">Let us begin with the definition of each which will be followed by examples:</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">1. Additive measures: These are those specific class of fact measures which can be aggreagated across all dimension and their hieracchy</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">2. Semi-Additive measures: These are those specific class of fact measures which can be aggreagated across all dimension and their hieracchy except the time dimension</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">3. Non-additive measures: These are those specific class of fact measures which cannot be aggreagated across all/any dimension and their hieracchy</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">For example:</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">A) We have sales figures...one may tend to add sales across all quareters to avail the yearly sales..hence this is an example of Additive measure</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">B) We have stock levels say 1000(qty of Item A) on Monday...I sell 200(qty of Item A) on Tuesday I further sell 300(qty of Item A) on Wednesday...going by basic math On Thursday I should be left with 500(qty of Item A, assuming no inventory has flown in) to obtain current stock level I cannot aggregate the Stock sales across time dimension hierarchy...If done I will have inappropriate outcomes</span><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><br style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;" /><span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">C) Aggregation of percentage or dates is an Ideal example of non-additive measures.</span><br />
<span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;"><br /></span>
<span style="background-color: #f4f8fb; font-family: Arial, sans-serif; font-size: 13px;">Happy Learning !!</span></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-81954574374404877462016-03-30T01:19:00.001-07:002016-03-30T01:19:18.638-07:00SQL Server 2008 Integration Services Tasks<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 style="background-color: #fefefe; color: #222222; font-family: Arial, Helvetica, sans-serif; font-weight: normal;">
SQL Server 2008 Integration Services Tasks</h1>
<div style="background-color: #fefefe; color: #333333; font-family: Arial, Helvetica, sans-serif; font-size: 12.8px;">
SQL Server Integration Services (SSIS) is a Business Intelligence tool used by database developers or administrators to perform extract transform & load (ETL) operations. SSIS has come long way since the early days of Data Transformation Services (DTS) which was initially introduced by Microsoft in SQL Server 7.0. Most database developers and administrators had their first interaction with DTS when using the Import and Export Wizard. The primary objective of DTS was to transform data from an OLEDB data sources to another destination. In SQL Server 2008 Integration Services, there are around 28 different control flow tasks and 11 Maintenance Plan Tasks. This article is the first in a series that will cover all the Control Flow Tasks and Maintenance Plan Tasks in SQL Server 2008 Integration Services. Beginning with an overview of the Control Flow Tasks and Maintenance Plans in SSIS 2008.<br />
Overview of Control Flow Tasks in SSIS<br />The Control Flow Tasks within SQL Server Integration Services (SSIS) can be grouped into 9 major categories based on usage. The different categories for control flow tasks are listed below:<br />
<ul>
<li style="margin-left: 15px;">Container Tasks</li>
<li style="margin-left: 15px;">Data Flow Tasks</li>
<li style="margin-left: 15px;">Data Preparation Tasks</li>
<li style="margin-left: 15px;">Workflow Tasks</li>
<li style="margin-left: 15px;">SQL Server Tasks</li>
<li style="margin-left: 15px;">WMI Tasks</li>
<li style="margin-left: 15px;">Scripting Tasks</li>
<li style="margin-left: 15px;">Backward Compatibility Tasks</li>
<li style="margin-left: 15px;">SQL Server Analysis Services Tasks</li>
</ul>
Container Tasks: – container control flow tasks within SSIS can be used by database developers or database administrators when providing a structure to the package and to repeat control flows within the package. It can also be used to group tasks and containers within a package into a meaningful unit of work. Different types of container tasks are For Loop Container, Foreach Loop Container, Sequence Container and Task Host. The Task Host container is not visible within the control flow toolbox, it is designed to provide service to a single task. The container tasks within SSIS are shown in the image below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEjs2LLYlXpsVwu-Hbircy9DoDWpNU3mxWb7AjTyaw3J2dR_zojUeGmugZXkX0yJJ6XmHBb7zeVMsu9G4tcMVcq0HADNKJfdrTnIUcxpNZnyf1vA9ckjBH2c-J6bEIr_BDOjT3xT7fLyhRgkGZlrQuDaybosN_pKszvnm9He44Bbcj5rHBpCuG4cr2EObacJl4MVh62IQWWzscRWFLNPhuSsUGdw0I8zor4aRkXk8lafKcYl68dWFg=s0-d-e1-ft" /><br /><br />While designing SSIS packages to group two or more tasks together; select the tasks , right click on them and select the Group option from the drop down. This will group the tasks together as a group as shown in the image below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEiJ4ucLNxJb7FEGCpkZX8ggWROehNIg6hJOIzHFIJ6TqnPxdVTa5DkYakBnVFdfz1SI2_qsVgqvKdPG9zUmBWYvaNPpF_1_IpIrkBJqLzy2xMXno2ZcE1J5CbqUklIweNSfDHwGCJ2_2ik7AyRNQxNflcNb-L9Vy9d9KleE_L5ZzWkxq7D4ICki_EYcdfSfN30Qe-NdzfsUVXsWy1_4x4OYhJelmzlbGfKQ6JgTGpiDAifpk3plfg=s0-d-e1-ft" /><br /><br />Data Flow Tasks: – Data flow tasks within SSIS can be used to move data from a source to destination and at the same time use certain transformation tasks to clean and modify the data while it is transferred. By adding a data flow task to the package control flow the package can be made to perform data extraction, transformation and load (ETL) operations. The data flow tasks available within SSIS are shown in the image below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEj-xyfXhI3-YoUSa8HFr2FWqUuU4Yv1LvF8f8TUIGUsY3TqqB2HaswZQPN3CyTPS2NT7JiG9vFDPStN-_gP2jsa4uWP9FNH3ZE-sxQpDINwYtffF1Zds7gfTDr5bv-dpoRi-Jg2ajQYOtLApzHTRcMMGYw094XItMsP0BjdKi_aWnO-TfGPFNCUKXG9B0riq9bcDV68nVXQZuZDmFs1ZDU2OuqfGmgEESXJry8vBus5Ynby0q-w4A=s0-d-e1-ft" /><br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEh-T3PTALf1W7MY828SVRQbBIovuOA_pmmhGLH1eANAbx9-x9DfQeHOA-VUIMI8hEoixCMKqhOmTV-AHLspYLr58iOVao85WRkXxEN8Gp0YdBrpbqjNn17MRrexP35PyUmYUS_NDB-uuyNa2dXLMQFNj9NvfaT3mTzI0w6mqSuUyT_P6nEEJ3B6vWgNHBhSasPvQ3y67HogQ_gj7Uh97tpQh2qqlRDbn6jwyHFMkxSWY9WjNugH6w=s0-d-e1-ft" /><br /><br />Data Preparation Tasks: – data preparation tasks within SSIS can be used to retrieve data or validate it to determine the quality of data. The different types of data preparation tasks available in SSIS are File System Task, FTP Task, Web Service Task, XML Task and Data Profiling Task. The list of data preparation tasks within SSIS are shown in the image below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEjDuVFeChNipGW9vkxl-QbUnSlsYpWZsSozrkMNNi4YXniTt4DOMd3k07-TaLj8IqYu9i1HLqtjcbxnsA7BZ3UfHwvGNHcL6K66X4wF877vb7iD3q0L-BRP53PvLDJypOurL9GF-vGt1ATAiY5pvRUTQYP97niAVywr0aJZCH85SCoh0GbP-_1OpF6_LSJuOSD5OWZlnxd5CsT3CwJJQg_TU5IZn8U9gfA6I6SihvtnnniTET4lHg=s0-d-e1-ft" /><br /><br />Workflow Tasks: – Workflow tasks within SSIS can be used to send email messages or to communicate with other processes to run other packages, run some other programs or batch files. Use Message Queue Task to send and receive messages between packages. The workflow tasks within SSIS are shown in the snippet below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEipdFnnt6bOw9pEfnmE0A_5K-t84R56gcUVwM00q4fSL2Xd-vppOje060SDEL-4pruswGmEB0jwrkO_dCAgvGcyvaVtjdf0CpmyMlJSckIduvwYbkKW6r0Ntg6wqzx99GBMgzBHSAdwoqiw2cNNxmHxRqpQZL8XLZx4MjjJKjPReLk8OciOITpJ1kJ2tP9fPhTpO4E16WUSHyd5eVyvrkab83C-DHqJb_pW_ogFqDR2bjADPnXS2Q=s0-d-e1-ft" /><br /><br />SQL Server Tasks: – SQL Server tasks within SSIS can be used to copy, modify and delete SQL Server objects and data. Use the Bulk Insert Task to insert data from a text file or a flat file into an SQL Server database table the same way that the BULK INSERT statement or the BCP.EXE command line tool is used. The Execute SQL Task can be used to execute any TSQL code such as truncating a staging table, running queries, stored procedures etc. The Transfer Database Task can be used to transfer database objects from one database to another. The Transfer Error Message Task can be used to transfer user defined error messages from one instance of SQL Server to another. The Transfer Job Task can be used to transfer SQL Server Agent Job between instances of SQL Server. The Transfer Login Task can be used to transfer logins between instances of SQL Server. The Transfer Master Stored Procedure Task can be used to transfer user defined stored procedures between the MASTER databases on instances of SQL Server. The Transfer SQL Server Objects Task can be used to transfer tables or stored procedures between instances of SQL Server.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEjtI-43-diLxq-qreLURcz7KQ8zI6NEa1Y2_HnuQ6DaK4D8kR6o25UXcRoLuKqzMMnVscT1mj8AFbpjT7-nti8dfjMdlmP7nu7xUA11rubfq7wVFpEJ1X-BtRHxf-y9NCkP2wjGpSk4RU2RpzXdnmfj6qbLUxbGGe_25ekdOS6DLmv_rIw36yd0uXdMsa5RET5axtwSwbBX953LgiARah6QZTaf11YEmo_0zAYXCOQ6kI_P8zrCrQ=s0-d-e1-ft" /><br /><br />WMI Tasks: - Windows Management Instrumentation (WMI) Tasks within SSIS can be used by to read WMI data and watch for WMI events. Different types of WMI tasks are WMI Data Reader Task and WMI Event Watcher Tasks. The WMI Data Reader Task can be used to run WQL queries against the windows management instrumentation to read the event logs, determine hardware information etc. The WMI Event Watcher Task empowers SSIS to wait for certain WMI events and then respond to the event. The WMI Tasks within SSIS are shown in the image below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEgH4fcD9LCoAIa2VNyiKIFVT4_Va1sfUNtSMWtlaiHPFkuZgdSrQNw7oSRCwYtVYUrQ4RA2Zhd0LU6zr2uw8iVNs8VF4Uw0vgVva1Phzr29Xibp8prWCPxO_Qn_4cypuZCTHpfzLeojtY8Mr9GEnOQGg-y481A3XAPuIS4_hqwWQN5nmjTNN7bGy39uRg92awYKo_SfuiZiEH-MyRj8fNv8ZVKb26H7dNwwDTJC4ZCY8ZYLvJqJIw=s0-d-e1-ft" /><br /><br />Scripting Tasks: – Scripting tasks within SSIS can be used to perform certain functions which are not fully supported by the existing tasks in SSIS.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEiqTSbjuqAe85iDYnasS5iY1qyh1RQ6zb6I0y_jbYbyVyASWCp1SaGh-SY3zNd3dIH-J4m0ucy7obNHdj3UMdMGh7cPU_IGZ3OsIPlIqqtjeo552S5XOotLzgreeZqo_1cjTmYX0ffeTlVLwOaAlwTTMTmwQ4J3Aw2N_sF5GGtw6r1FfdZZDuZBZDFU413Kgn4VzbElPPEbqXm5AKNJ3jDhUKHgKah0wzjPUVa7k6I_c4_mk95txw=s0-d-e1-ft" /><br /><br />Backward Compatibility Tasks: – Backward compatibility tasks within SSIS can be used to execute legacy application scripts with the SSIS packages. The different types of backward compatibility tasks are ActiveX Script Task and the Execute DTS 2000 Package Task. The ActiveX Script Tasks is deprecated and is included in SSIS 2005 and SSIS 2008 for backward compatibility with DTS 2000. The Execute DTS 2000 Package Tasks can be used to execute DTS packages (SQL Server 2000) from within an SSIS 2005 or SSIS 2008 packages.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEi-kFRxRH-uTOtNnv28SoQevdVA_hP5FiUrGLw2zeDgo8ib3o8anCt5kJypKoaTZa1_4LjG15Tfm8-DlqGlqWT52hW9EzMqCzwBS7WWcmQfqEVeMD9CDtwpz82-Co22-MDbWQEUpu2QNs9C2su75e2aOjyx-LxsD4itly9Ll8sCFCZNzuQjmeA_2z1DU5WsV8K7Y8u4-Dnjf03cymEfsFQOaSR7tn6RddAzvYHxbfpBw4vBUma0Ru4=s0-d-e1-ft" /><br /><br />SQL Server Analysis Services Tasks: – SQL Server Analysis Services Tasks within SSIS can be used to create, modify, delete and process Analysis Services objects. The different types of Analysis Services tasks are Analysis Services Execute DDL Task, Analysis Services Processing Task and Data Mining Query Tasks. The SQL Server Analysis Services Execute DDL Task is much similar to Execute SQL Task; however use of Analysis Services Execute DDL Task can issue Data Definition Language statements against an Analysis Services system. The DDL statements can be used to create cubes, dimensions, KPI’s or any other analytical processing OLAP objects. The Analysis Services Processing Task can be used to process analysis services objects such as cubes, dimensions and mining models. The Data Mining Query Task can be used to run prediction queries based on data mining models built in analysis services. A prediction query creates a prediction for new data by using the mining models chosen.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEiM9ThLfXUPMtOp1arKRm7BXBJRwUNZlo4k4RnbihP1MJQzHfOnGVXMEjDsblv1SrP2Z_m7u6-pP9xde0dxrB3ae2MYojXDkRELeH5cgY-xbyLeJ6feoLJRDG5veB3NQ-kGpbHJXvXm7iClIfFiMnmhMnpiQLeQZE-BPvfjBaXLTIzp6RN3HtQjdP7tDJGCzikeG21xWqzu-rQb-sV-JiD0-Pt_fMAnFDRW5OoUaiYFdTWoX-Xr_lM=s0-d-e1-ft" /><br /><br />Overview of Maintenance Plan Tasks in SSIS<br />Within SQL Server 2008 Integration Services there are around 11 Maintenance Plan Tasks available. Different types of Maintenance Plan Tasks are shown in the image below.<br /><br /><img class="CToWUd" src="https://blogger.googleusercontent.com/img/proxy/AVvXsEjtrUZszExqNKHwagFjk96N91MJftq-KSUhPJ4ufG7_YPKcR3LkBXvAAeqhAtdNGD_LN8SHLoA4B9XbZoGN0N0lFlN8Wq2jWeP4UFts8J2ZLqsUt3ekWLiuM37tBYayGz5nS-1EbjTJ7lLC1imXCzO9yInigMEGvtbjJPjDvP4Z483DvtZcXrdTfKINqsxco03bEQfh77Sk3rzMXDjTu_vcttru55mUXijOufBMASot_Ugk_Yt_OTo=s0-d-e1-ft" /><br /><br />Database Administrators can create database maintenance plans either by using Maintenance Plan Wizard or by using SSIS designer. By using the Maintenance Plan Wizard a very basic maintenance plan can be created for all the system and user databases. However, to create an enhanced workflow it is advised to create maintenance plan using SSIS designer. To learn more about creating a Maintenance Plan, refer to the earlier article titled Overview of Maintenance Plans in SQL Server 2008.<br /><br />Back Up Database Task: – Back Up database task different types of database backups to be performed; such as Full, Differential or Transactional Log backups based on the Recovery Model of the System or User databases. To learn more about the different recovery models in SQL Server refer to the article titled “Database Recovery Models in SQL Server”.<br /><br />Check Database Integrity Task: – Check Database Integrity Task can be used to check the allocation and structural integrity of all the user and system tables within a database. This task also has an option to check the allocation and structural integrity of all the indexes available within a database. This task internally executes DBCC CHECKDB statement.<br /><br />Execute SQL Server Agent Job Task: – Execute SQL Server Agent Job Task can be used to run SQL Server Agent Jobs that are created on the SQL Server Instance.<br /><br />Execute T-SQL Statement Task: – Execute T-SQL Statement Task can be used to execute some Transact SQL queries against databases. This task is only available when creating Maintenance Plans using SSIS designer.<br /><br />History Cleanup Task: - History Cleanup Task deletes the historical data related to database backups and restore activities; SQL Server Agent Job history, database maintenance plan history etc. This task basically uses sp_delete_backuphistory system stored procedure to clean up the history prior to the number of days, weeks or months from the current system date.<br /><br />Maintenance Cleanup Task: – Maintenance Cleanup Task can be used to remove the older files like maintenance plan execution reports, database backup files etc. Use this task when creating maintenance plans it will remove old files which are not required. <br /><br />Notify Operator Task: – Notify Operator Task can be used to send messages to the SQL Server Agent Operator when a task has successfully completed or failed. The operator can be notified by an email, pager or by net send method.<br /><br />Reorganize Index Task: – Reorganize Index Task is used to defragment & compact clustered and non-clustered indexes on tables and views. The index reorganise is best suited when the index are not heavily fragmented. This process usees fewer system resources compared to rebuilding an index. If the indexes are heavily fragmented then it is best to rebuild indexes using the Rebuild Index Task.<br /><br />Rebuild Index Task: – Rebuild Index task can be used to organise data on the data and index pages by rebuilding indexes. This helps to improve the performance of index seeks and scans. This task also optimises the distribution of data and free space on the index pages, thereby allowing faster future growth. If this task is used to rebuild indexes in a single database, it allows selection of views and tables to rebuild the index. This task also has options such as “Sort results in tempdb” and “Keep index online while reindexing”. However these operations require sufficient disk space in TempDB database. <br /><br />Shrink Database Task: – Shrink Database Task can be used to reduce the disk space which is consumed by the database and log files by removing the empty data and log pages. Using this task the space gained after shrinking the database can either be returned to the operating system or it can be retained within the database for its future growth. This task executes the DBCC SHRINKDATABASE statement.<br /><br />Update Statistics Task: – Update Statistics task ensures that the query optimiser has up-to-date information about the distribution of data values within the tables. This allows the optimizer to make better judgments about the data access strategies it needs to choose. The Update Statistics task basically executes UPDATE STATISTICS statement.<br /><br /><b><u><i>Conclusion - </i></u></b><br />
<b><br /></b>This introductory article has contained an overview of the 28 different control flow tasks and 11 Maintenance Plan Tasks which are available in SQL Server 2008 Integration Services. The next articles in this series will walk through the steps to configure and use all the above mentioned Control Flow and Maintenance Plan Tasks.</div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-84509772281795847942014-06-14T22:58:00.002-07:002014-06-14T22:58:58.080-07:00Use of Script Task to create SQL table using VB.Net in SSIS<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal">
The Below Code will be useful in the cases when we need to deal with flat files, wherein the file structure may be dynamic and we are dealing with multiple files each one with a different schema definition.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The approach will be here is to loop through each file and then read the first line of each file and read the column names from the file and then dynamically create the table and then load the data.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
The below code is used to read the column information from the file and then generate a SQL Command which will create the table in the database.</div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
Here it goes - </div>
<div class="MsoNormal">
<b><u><span lang="EN-US"><br /></span></u></b></div>
<div class="MsoNormal">
<b><u><span lang="EN-US">Code
to dynamically create a table using the Input File Data –<o:p></o:p></span></u></b></div>
<div class="MsoNormal">
<b><u><span lang="EN-US"><br /></span></u></b></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">Public</span><span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
<span style="color: blue;">Sub</span> Main()</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Dim</span>
objReader <span style="color: blue;">As</span> <span style="color: blue;">New</span>
StreamReader(Dts.Variables(<span style="color: #a31515;">"SRCDATAFILE"</span>).Value.ToString)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Dim</span>
sLine <span style="color: blue;">As</span> <span style="color: blue;">String</span>
= <span style="color: #a31515;">""<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Dim</span>
arrText <span style="color: blue;">As</span> <span style="color: blue;">New</span>
ArrayList()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> sLine = objReader.ReadLine()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">'MsgBox(sLine)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> sLine = sLine.Replace(ControlChars.Tab,
<span style="color: #a31515;">"|"</span>)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">'MsgBox(sLine)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> sLine = Trim(sLine.Replace(<span style="color: #a31515;">" |"</span>, <span style="color: #a31515;">"|"</span>).Replace(<span style="color: #a31515;">"| "</span>, <span style="color: #a31515;">"|"</span>))<o:p></o:p></span></div>
<div class="MsoNormal" style="margin: 0cm 0cm 0.0001pt 36pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> sLine =
<span style="color: #a31515;">"IF EXISTS(SELECT 1 FROM
INFORMATION_SCHEMA.TABLES WHERE
TABLE_NAME = My_Table') DROP TABLE RBI_Product_stage_tmp; Create Table My_Table
(["</span> & Replace(sLine, <span style="color: #a31515;">"|"</span>,
<span style="color: #a31515;">"] Varchar(150), ["</span>) & <span style="color: #a31515;">"] Varchar(150))"<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">'MsgBox(sLine)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Dts.Variables(<span style="color: #a31515;">"CREATESQLCMD"</span>).Value
= sLine<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">If</span> <span style="color: blue;">CBool</span>(Dts.Variables(<span style="color: #a31515;">"I"</span>).Value)
<span style="color: blue;">Then<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Dts.Variables(<span style="color: #a31515;">"CREATESQLCMD"</span>).Value = <span style="color: #a31515;">""<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">End</span> <span style="color: blue;">If<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> objReader.Close()<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Dts.TaskResult = ScriptResults.Success<o:p></o:p></span></div>
<span lang="EN-US" style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-fareast; mso-no-proof: yes;">End</span><span lang="EN-US" style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: "Times New Roman"; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-fareast; mso-no-proof: yes;"> <span style="color: blue;">Sub</span></span><div>
<br /></div>
<div>
Happy Learning !!</div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-17696871413848357402013-08-14T00:46:00.002-07:002013-08-14T00:46:06.251-07:00Scripting Indexes in SQL 2000<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
IndexCursor <span style="color: blue;">CURSOR</span> <span style="color: blue;">FOR<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">OBJECT_NAME</span><span style="color: grey;">(</span>SI<span style="color: grey;">.</span>ID<span style="color: grey;">),</span> SI<span style="color: grey;">.</span>ID<span style="color: grey;">,</span> SI<span style="color: grey;">.</span>Name<span style="color: grey;">,</span> SI<span style="color: grey;">.</span>indid<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> dbo<span style="color: grey;">.</span><span style="color: green;">sysindexes</span> SI<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">LEFT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN</span> <span style="color: green;">INFORMATION_SCHEMA</span><span style="color: grey;">.</span><span style="color: green;">TABLE_CONSTRAINTS</span>
TC<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ON</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> SI<span style="color: grey;">.</span>Name <span style="color: grey;">=</span> TC<span style="color: grey;">.</span>CONSTRAINT_NAME <span style="color: grey;">AND</span> <span style="color: magenta;">OBJECT_NAME</span><span style="color: grey;">(</span>SI<span style="color: grey;">.</span>ID<span style="color: grey;">)</span> <span style="color: grey;">=</span> TC<span style="color: grey;">.</span>TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> TC<span style="color: grey;">.</span>CONSTRAINT_NAME <span style="color: grey;">IS</span> <span style="color: grey;">NULL<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AND</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">OBJECTPROPERTY</span><span style="color: grey;">(</span>SI<span style="color: grey;">.</span>ID<span style="color: grey;">,</span> <span style="color: red;">'IsUserTable'</span><span style="color: grey;">)</span> <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AND</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">INDEXPROPERTY</span><span style="color: grey;">(</span> SI<span style="color: grey;">.</span>[id]<span style="color: grey;">,</span> SI<span style="color: grey;">.</span>[name]<span style="color: grey;">,</span> <span style="color: red;">'IsStatistics'</span> <span style="color: grey;">)</span> <span style="color: grey;">=</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AND</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">OBJECT_NAME</span><span style="color: grey;">(</span>SI<span style="color: grey;">.</span>ID<span style="color: grey;">)</span> <span style="color: grey;">in</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: red; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">'Table1'</span><span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">,<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: red; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">'Table2'<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BY</span> <span style="color: magenta;">OBJECT_NAME</span><span style="color: grey;">(</span>SI<span style="color: grey;">.</span>ID<span style="color: grey;">),</span> SI<span style="color: grey;">.</span>indid<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @IxTable <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @IxTableID
<span style="color: blue;">INT<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @IxName <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @IxID <span style="color: blue;">INT<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">-- Loop through
all indexes<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">OPEN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
IndexCursor<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FETCH</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span> IndexCursor
<span style="color: blue;">INTO</span> @IxTable<span style="color: grey;">,</span>
@IxTableID<span style="color: grey;">,</span> @IxName<span style="color: grey;">,</span>
@IxID<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHILE </span><span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(</span><span style="color: magenta; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">@@FETCH_STATUS</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">=</span> 0<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
@IXSQL <span style="color: blue;">NVARCHAR</span><span style="color: grey;">(</span>4000<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span> @IXSQL <span style="color: grey;">=</span> <span style="color: red;">'CREATE '<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- Check if the
index is unique<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF </span><span style="color: grey;">(</span><span style="color: magenta;">INDEXPROPERTY</span><span style="color: grey;">(</span>@IxTableID<span style="color: grey;">,</span> @IxName<span style="color: grey;">,</span> <span style="color: red;">'IsUnique'</span><span style="color: grey;">)</span> <span style="color: grey;">=</span> 1<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span>
<span style="color: red;">'UNIQUE '<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- Check if the
index is clustered<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF </span><span style="color: grey;">(</span><span style="color: magenta;">INDEXPROPERTY</span><span style="color: grey;">(</span>@IxTableID<span style="color: grey;">,</span> @IxName<span style="color: grey;">,</span> <span style="color: red;">'IsClustered'</span><span style="color: grey;">)</span> <span style="color: grey;">=</span> 1<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span>
<span style="color: red;">'CLUSTERED '<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span> @IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span> <span style="color: red;">'INDEX '</span> <span style="color: grey;">+</span> @IxName <span style="color: grey;">+</span> <span style="color: red;">' ON ['</span> <span style="color: grey;">+</span> @IxTable <span style="color: grey;">+</span> <span style="color: red;">'] ('<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- Get all columns
of the index<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--DECLARE
IndexCursorColumn CURSOR FOR<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--SELECT
SC.Name,0,0<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--FROM
dbo.sysindexkeys IC<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--JOIN
dbo.syscolumns SC ON IC.ID = SC.ID AND IC.Column_ID = SC.Column_ID<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--WHERE IC.ID =
@IxTableID AND indid = @IxID<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--ORDER BY
IC.Index_Column_ID--,IC.is_included_column<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
IndexCursorColumn <span style="color: blue;">CURSOR</span> <span style="color: blue;">FOR<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SELECT</span> SC<span style="color: grey;">.</span>Name<span style="color: grey;">,</span>0<span style="color: grey;">,</span>0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span> dbo<span style="color: grey;">.</span><span style="color: green;">sysindexkeys</span> IC<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN</span> dbo<span style="color: grey;">.</span><span style="color: green;">syscolumns</span> SC <span style="color: blue;">ON</span> IC<span style="color: grey;">.</span>ID <span style="color: grey;">=</span> SC<span style="color: grey;">.</span>ID <span style="color: grey;">AND</span> IC<span style="color: grey;">.</span>colid <span style="color: grey;">=</span> SC<span style="color: grey;">.</span>colid<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">WHERE</span> IC<span style="color: grey;">.</span>ID <span style="color: grey;">=</span> @IxTableID <span style="color: grey;">AND</span> indid <span style="color: grey;">=</span> @IxID<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ORDER</span> <span style="color: blue;">BY</span> IC<span style="color: grey;">.</span>indid<span style="color: green;">--,IC.is_included_column<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
@IxColumn <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
@IxIncl <span style="color: blue;">bit<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
@Desc <span style="color: blue;">bit<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
@IxIsIncl <span style="color: blue;">bit</span> <span style="color: blue;">set</span>
@IxIsIncl <span style="color: grey;">=</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DECLARE</span>
@IxFirstColumn <span style="color: blue;">BIT</span> <span style="color: blue;">SET</span>
@IxFirstColumn <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- Loop throug all
columns of the index and append them to the CREATE statement<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">OPEN</span>
IndexCursorColumn<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FETCH</span> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span>
IndexCursorColumn <span style="color: blue;">INTO</span> @IxColumn<span style="color: grey;">,</span> @IxIncl<span style="color: grey;">,</span> @Desc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">WHILE </span><span style="color: grey;">(</span><span style="color: magenta;">@@FETCH_STATUS</span> <span style="color: grey;">=</span> 0<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF </span><span style="color: grey;">(</span>@IxFirstColumn <span style="color: grey;">=</span> 1<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IxFirstColumn <span style="color: grey;">=</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ELSE<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--check
to see if it's an included column<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF </span><span style="color: grey;">((</span>@IxIsIncl <span style="color: grey;">=</span> 0<span style="color: grey;">)</span> <span style="color: grey;">AND</span><span style="color: blue;"> </span><span style="color: grey;">(</span>@IxIncl <span style="color: grey;">=</span> 1<span style="color: grey;">))<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IxIsIncl <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span>
<span style="color: red;">') INCLUDE ('<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ELSE<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span>
<span style="color: red;">', '<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span>
<span style="color: red;">'['</span> <span style="color: grey;">+</span> @IxColumn <span style="color: grey;">+</span> <span style="color: red;">']'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--check
to see if it's DESC<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF</span>
@Desc <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span>
@IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span>
<span style="color: red;">' DESC'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FETCH</span> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span>
IndexCursorColumn <span style="color: blue;">INTO</span> @IxColumn<span style="color: grey;">,</span> @IxIncl<span style="color: grey;">,</span> @Desc<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">CLOSE</span>
IndexCursorColumn<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">DEALLOCATE</span>
IndexCursorColumn<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SET</span> @IXSQL <span style="color: grey;">=</span> @IXSQL <span style="color: grey;">+</span> <span style="color: red;">')'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">-- Print out the
CREATE statement for the index<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">PRINT</span> @IXSQL<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FETCH</span> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span> IndexCursor
<span style="color: blue;">INTO</span> @IxTable<span style="color: grey;">,</span>
@IxTableID<span style="color: grey;">,</span> @IxName<span style="color: grey;">,</span>
@IxID<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CLOSE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
IndexCursor<o:p></o:p></span></div>
<br />
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">DEALLOCATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> IndexCursor</span><o:p></o:p></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-16196684360335393972013-08-14T00:44:00.001-07:002013-08-14T00:44:33.152-07:00Generating a Table Creation Script using SQL Query<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@TABLE_NAME <span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span>100<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@CUR_TABLE_NAME <span style="color: blue;">CURSOR</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DELETE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span> ISGSQLWT01<span style="color: grey;">.</span>MONet<span style="color: grey;">.</span>dbo<span style="color: grey;">.</span>Table_Schema_Info_Prod<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@CUR_TABLE_NAME <span style="color: grey;">=</span> <span style="color: blue;">CURSOR</span>
<span style="color: blue;">FOR<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> TABLE_NAME
<span style="color: blue;">FROM</span> <span style="color: green;">INFORMATION_SCHEMA</span><span style="color: grey;">.</span><span style="color: green;">TABLES</span> <span style="color: blue;">where</span> table_name <span style="color: grey;">in</span><span style="color: blue;"> </span><span style="color: grey;">(</span><span style="color: red;">'TableName1'</span><span style="color: grey;">,</span><span style="color: red;">'TableName2'</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">OPEN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@CUR_TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FETCH</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">NEXT<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@CUR_TABLE_NAME <span style="color: blue;">INTO</span> @TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">WHILE</span> <span style="color: magenta;">@@FETCH_STATUS</span>
<span style="color: grey;">=</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BEGIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">INSERT</span> <span style="color: blue;">INTO</span> ISGSQLWT01<span style="color: grey;">.</span>MONet<span style="color: grey;">.</span>dbo<span style="color: grey;">.</span>Table_Schema_Info_Prod<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SELECT</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @TABLE_NAME <span style="color: blue;">as</span>
<span style="color: red;">'Table Name'</span><span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> c<span style="color: grey;">.</span>name <span style="color: blue;">as</span> <span style="color: red;">'Column Name'</span><span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> t<span style="color: grey;">.</span>Name <span style="color: blue;">as</span> <span style="color: red;">'Data type'</span><span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> c<span style="color: grey;">.</span>[length] <span style="color: blue;">as</span> <span style="color: red;">'Max Length'</span><span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> c<span style="color: grey;">.</span>[prec] <span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">isnull</span><span style="color: grey;">(</span>c<span style="color: grey;">.</span>scale<span style="color: grey;">,</span>0<span style="color: grey;">)</span> <span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">columnproperty</span><span style="color: grey;">(</span>c<span style="color: grey;">.</span>id<span style="color: grey;">,</span>c<span style="color: grey;">.</span>name<span style="color: grey;">,</span><span style="color: red;">'AllowsNull'</span><span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">columnproperty</span><span style="color: grey;">(</span>c<span style="color: grey;">.</span>id<span style="color: grey;">,</span>c<span style="color: grey;">.</span>name<span style="color: grey;">,</span><span style="color: red;">'IsIdentity'</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">syscolumns</span> c
<span style="color: grey;">,</span> <span style="color: green;">systypes</span> t<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">WHERE<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> c<span style="color: grey;">.</span>xtype <span style="color: grey;">=</span> t<span style="color: grey;">.</span>xtype <span style="color: grey;">AND<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">OBJECT_NAME</span><span style="color: grey;">(</span>c<span style="color: grey;">.</span>id<span style="color: grey;">)</span> <span style="color: grey;">=</span> @TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FETCH</span> <span style="color: blue;">NEXT</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span> @CUR_TABLE_NAME <span style="color: blue;">INTO</span>
@TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">END<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CLOSE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@CUR_TABLE_NAME<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">DEALLOCATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> @CUR_TABLE_NAME</span><o:p></o:p></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-3340218227248423622013-03-28T21:32:00.002-07:002013-03-28T21:33:36.980-07:00Basics of SQL Server Job Scheduling<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<h3>
<span lang="EN-US" style="font-size: 10.5pt;">To create and attach a schedule
to a job<o:p></o:p></span></h3>
<div style="margin-left: 36.0pt; mso-list: l2 level1 lfo1; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">1.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">In <span class="label">Object Explorer,</span> connect to an
instance of the SQL Server Database Engine, and then expand that instance.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l2 level1 lfo1; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">2.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Expand <span class="label">SQL Server Agent</span>, expand <span class="label">Jobs</span>, right-click the job you want to schedule, and click <span class="label">Properties</span>.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l2 level1 lfo1; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">3.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Select the <span class="label">Schedules</span> page, and then click
<span class="label">New</span>.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l2 level1 lfo1; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">4.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">In the <span class="label">Name</span> box, type a name for the new
schedule. <o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l2 level1 lfo1; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">5.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Clear the <span class="label">Enabled</span> check box if you do not
want the schedule to take effect immediately following its creation.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l2 level1 lfo1; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">6.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">For <span class="label">Schedule Type</span>, select one of the
following: <o:p></o:p></span></div>
<div style="margin-left: 69.5pt; mso-list: l2 level2 lfo1; tab-stops: list 72.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Click <span class="label">Start
automatically when SQL Server Agent starts</span> to start the job when the SQL
Server Agent service is started.<o:p></o:p></span></div>
<div style="margin-left: 69.5pt; mso-list: l2 level2 lfo1; tab-stops: list 72.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Click <span class="label">Start
whenever the CPUs become idle</span> to start the job when the CPUs reach an
idle condition.<o:p></o:p></span></div>
<div style="margin-left: 69.5pt; mso-list: l2 level2 lfo1; tab-stops: list 72.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Click <span class="label">Recurring</span>
if you want a schedule to run repeatedly. To set the recurring schedule,
complete the <span class="label">Frequency</span>, <span class="label">Daily
Frequency</span>, and <span class="label">Duration</span> groups on the dialog.<o:p></o:p></span></div>
<div style="margin-left: 69.5pt; mso-list: l2 level2 lfo1; tab-stops: list 72.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Click <span class="label">one
time</span> if you want the schedule to run only once. To set the <span class="label">One time</span> schedule, complete the <span class="label">One-time
occurrence</span> group on the dialog.<o:p></o:p></span><br />
<span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;"><br /></span></div>
<h3>
<span lang="EN-US" style="font-size: 10.5pt;">To attach a schedule to a job<o:p></o:p></span></h3>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">1.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">In <span class="label">Object Explorer,</span> connect to an
instance of the SQL Server Database Engine, and then expand that instance.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">2.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Expand <span class="label">SQL Server Agent</span>, expand <span class="label">Jobs</span>, right-click the job that you want to schedule, and
click <span class="label">Properties</span>.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">3.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Select the <span class="label">Schedules</span> page, and then click
<span class="label">Pick</span>.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">4.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Select the schedule that you want to attach, and then click <span class="label">OK</span>.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">5.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">In the <span class="label">Job Properties</span> dialog box,
double-click the attached schedule.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">6.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Verify that <span class="label">Start date</span> is set correctly.
If it is not, set the date when you want for the schedule to start, and then
click <span class="label">OK</span>.<o:p></o:p></span></div>
<div style="margin-left: 36.0pt; mso-list: l1 level1 lfo2; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">7.<span style="font-family: 'Times New Roman'; font-size: 7pt;"> </span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">In the <span class="label">Job Properties</span> dialog box, click <span class="label">OK</span>. <o:p></o:p></span><br />
<span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;"><br /></span></div>
<h3>
<span lang="EN-US" style="font-size: 10.5pt;">To prevent a scheduled job from
running, you must do one of the following:<o:p></o:p></span></h3>
<div style="margin-left: 33.5pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Disable the schedule.<o:p></o:p></span></div>
<div style="margin-left: 33.5pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Disable the job.<o:p></o:p></span></div>
<div style="margin-left: 33.5pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Detach the schedule from the
job.<o:p></o:p></span></div>
<div style="margin-left: 33.5pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Stop the SQL Server Agent
service.<o:p></o:p></span></div>
<div style="margin-left: 33.5pt; mso-list: l0 level1 lfo3; tab-stops: list 36.0pt; text-indent: -18.0pt;">
<!--[if !supportLists]--><span lang="EN-US" style="font-family: Symbol; font-size: 10pt;">·<span style="font-family: 'Times New Roman'; font-size: 7pt;">
</span></span><!--[endif]--><span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">Delete the schedule.<o:p></o:p></span></div>
<span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">If the schedule is not enabled, the job can still run in response
to an alert or when a user runs the job manually. When a job schedule is not
enabled, the schedule is not enabled for any job that uses the schedule.<o:p></o:p></span><br />
<span lang="EN-US" style="font-family: Verdana, sans-serif; font-size: 9.5pt;">You must explicitly re-enable a schedule that has been disabled.
Editing the schedule does not automatically re-enable the schedule.<o:p></o:p></span></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-11416657709926885632013-03-25T07:52:00.001-07:002013-03-25T07:52:09.544-07:00Deleting Duplicate Data From SQL Server Table<div dir="ltr" style="text-align: left;" trbidi="on">
Duplicate data is a very well known and frequently occurring issue in any DBMS and dealing with deletion of duplicate data is something not that easy for one specially one who is dealing with crucial and huge data.<br />
<br />
There are 2 scenarios which can come while deleting duplicate data -<br />
<br />
1. when we have an identity column in our table containing duplicates.<br />
<br />
The problem becomes much easier here and we can just keep the data with MIN or MAX keys removing all other.<br />
<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Create New
Table<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">create</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">table</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ID <span style="color: blue;">int</span>
<span style="color: blue;">identity</span><span style="color: grey;">(</span>1<span style="color: grey;">,</span>1<span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">Name <span style="color: blue;">varchar</span><span style="color: grey;">(</span>30<span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">age <span style="color: blue;">int<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Insert Data
into Table with multiple Duplicate entries<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Ashish'</span><span style="color: grey;">,</span>24<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Ashish'</span><span style="color: grey;">,</span>24<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Gaurav'</span><span style="color: grey;">,</span>26<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check the data
before deleting duplicates<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">*</span> <span style="color: blue;">from</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DELETE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> ID <span style="color: grey;">NOT</span> <span style="color: grey;">IN</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">MIN</span><span style="color: grey;">(</span>ID<span style="color: grey;">)</span> <span style="color: blue;">FROM</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GROUP</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BY</span> Name<span style="color: grey;">,</span>age <span style="color: blue;">HAVING</span> <span style="color: magenta;">COUNT</span><span style="color: grey;">(</span>1<span style="color: grey;">)</span> <span style="color: grey;">></span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">UNION<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">MIN</span><span style="color: grey;">(</span>ID<span style="color: grey;">)</span> <span style="color: blue;">FROM</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GROUP</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BY</span> Name<span style="color: grey;">,</span>age <span style="color: blue;">HAVING</span> <span style="color: magenta;">COUNT</span><span style="color: grey;">(</span>1<span style="color: grey;">)</span> <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check the data
After deleting duplicates<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: grey;">*</span> <span style="color: blue;">from</span> Person</span><o:p></o:p></div>
<br />
<br />
Use Below Query --<br />
<br />
2. when we don't have any unique identifier in our table and it's a bit tricky here -<br />
<br />
there are 2 ways of doing it here again --<br />
<br />
(i) using the Row_Number() function comes with only SQL Server 2005 and later versions<br />
<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check if Table
already exists <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">IF</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">EXISTS(</span><span style="color: blue;">select</span> <span style="color: grey;">*</span> <span style="color: blue;">from</span> <span style="color: green;">INFORMATION_SCHEMA</span><span style="color: grey;">.</span><span style="color: green;">TABLES</span> <span style="color: blue;">where</span>
TABLE_NAME <span style="color: grey;">=</span> <span style="color: red;">'Person'</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--Drop if already exists<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Drop</span> <span style="color: blue;">Table</span>
Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Create New
Table<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">create</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">table</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">Name <span style="color: blue;">varchar</span><span style="color: grey;">(</span>30<span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">age <span style="color: blue;">int<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Insert Data
into Table with multiple Duplicate entries<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Ashish'</span><span style="color: grey;">,</span>24<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Ashish'</span><span style="color: grey;">,</span>24<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Gaurav'</span><span style="color: grey;">,</span>26<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check the data
before deleting duplicates<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">*</span> <span style="color: blue;">from</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">;</span><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WITH</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> CTE<span style="color: blue;"> </span><span style="color: grey;">(</span>Name<span style="color: grey;">,</span>Age<span style="color: grey;">,</span> DuplicateCount<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AS<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Name<span style="color: grey;">,</span>Age<span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: magenta; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ROW_NUMBER</span><span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">()</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">OVER</span><span style="color: grey;">(</span><span style="color: blue;">PARTITION</span> <span style="color: blue;">BY</span> Name<span style="color: grey;">,</span>Age <span style="color: blue;">ORDER</span> <span style="color: blue;">BY</span> Name<span style="color: grey;">)</span> <span style="color: blue;">AS</span> DuplicateCount<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DELETE<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> CTE<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
DuplicateCount <span style="color: grey;">></span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">GO<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check the data
After deleting duplicates<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: grey;">*</span> <span style="color: blue;">from</span> Person</span><o:p></o:p></div>
<br />
<br />
(ii) Here is some piece of code which i will suggest as best to deal with this and can be used anywhere in even the older version of SQL Server like 2000 without using any special functions.<br />
<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check if Table
already exists <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">IF</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">EXISTS(</span><span style="color: blue;">select</span> <span style="color: grey;">*</span> <span style="color: blue;">from</span> <span style="color: green;">INFORMATION_SCHEMA</span><span style="color: grey;">.</span><span style="color: green;">TABLES</span> <span style="color: blue;">where</span>
TABLE_NAME <span style="color: grey;">=</span> <span style="color: red;">'Person'</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--Drop if already exists<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Drop</span> <span style="color: blue;">Table</span>
Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Create New
Table<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">create</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">table</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">Name <span style="color: blue;">varchar</span><span style="color: grey;">(</span>30<span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">age <span style="color: blue;">int<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Insert Data
into Table with multiple Duplicate entries<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Ashish'</span><span style="color: grey;">,</span>24<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Ashish'</span><span style="color: grey;">,</span>24<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Gaurav'</span><span style="color: grey;">,</span>26<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">insert</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">into</span> Person<span style="color: grey;">(</span>Name<span style="color: grey;">,</span>age<span style="color: grey;">)</span> <span style="color: blue;">values</span><span style="color: grey;">(</span><span style="color: red;">'Nayan'</span><span style="color: grey;">,</span>25<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check the data
before deleting duplicates<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">*</span> <span style="color: blue;">from</span> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Declare 2
variables as @name (which is duplicate key) and @duplicateCount as Number of
Duplicates<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @name <span style="color: blue;">varchar</span><span style="color: grey;">(</span>30<span style="color: grey;">),</span> @duplicateCount <span style="color: blue;">int<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Declare a
Cursor to iterate through each duplicate name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
Cursor_Person <span style="color: blue;">CURSOR</span> <span style="color: blue;">FOR</span>
<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Name<span style="color: grey;">,(</span><span style="color: magenta;">count</span><span style="color: grey;">(</span>1<span style="color: grey;">)-</span>1<span style="color: grey;">)</span> cnt<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">from</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">group</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">by</span> Name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">having</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">count</span><span style="color: grey;">(</span>1<span style="color: grey;">)</span> <span style="color: grey;">></span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">OPEN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
Cursor_Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FETCH</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span>
Cursor_Person <span style="color: blue;">INTO</span> @name<span style="color: grey;">,</span> @duplicateCount<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHILE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">@@FETCH_STATUS</span> <span style="color: grey;">=</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--delete all
duplicate entries for each duplicate key keeping only one row<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">delete</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">top </span><span style="color: grey;">(</span>@duplicateCount<span style="color: grey;">)</span> <span style="color: blue;">from</span> Person <span style="color: blue;">where</span> Name <span style="color: grey;">=</span> @name<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FETCH</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">NEXT</span> <span style="color: blue;">FROM</span>
Cursor_Person <span style="color: blue;">INTO</span> @name<span style="color: grey;">,</span> @duplicateCount<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CLOSE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
Cursor_Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DEALLOCATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
Cursor_Person<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: green; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">--Check the data
After deleting duplicates<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: grey;">*</span> <span style="color: blue;">from</span> Person</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"><br /></span></div>
<div class="MsoNormal">
</div>
Please do comment if you find this post useful, suggestion are appreciated.<br />
Happy Coding !!<br />
<br />
<br />
<br /></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com2tag:blogger.com,1999:blog-35883176724266705.post-59456769202198833202013-03-25T04:30:00.003-07:002013-03-25T04:30:30.521-07:00Generic Function for Padding a Character in SQL Server<div dir="ltr" style="text-align: left;" trbidi="on">
Padding character to a string is something which we use quite often in our day to day development activities and is what i found as a very good generic way of doing this -<div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">CREATE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FUNCTION</span> dbo<span style="color: grey;">.</span>fn_PadValue<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @InValue <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">),</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @PadLimit <span style="color: blue;">int</span><span style="color: grey;">,</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @PadChar <span style="color: blue;">char</span><span style="color: grey;">(</span>1<span style="color: grey;">),</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @PadSide <span style="color: blue;">char</span><span style="color: grey;">(</span>1<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">RETURNS</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">AS<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Declare</span>
@Result <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Declare</span>
@Padding <span style="color: blue;">int<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">--Determine the
amount of padding<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Set</span> @Padding <span style="color: grey;">=</span> @PadLimit <span style="color: grey;">-</span> <span style="color: magenta;">Len</span><span style="color: grey;">(</span>@InValue<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF</span> @Padding <span style="color: grey;"><</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Set</span>
@Padding <span style="color: grey;">=</span> 0<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF</span> @PadSide <span style="color: grey;">=</span> <span style="color: red;">'L'</span> <span style="color: green;">-- Pad the left side<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Set</span> @Result
<span style="color: grey;">=</span> <span style="color: magenta;">Replicate</span><span style="color: grey;">(</span>@PadChar<span style="color: grey;">,</span> @Padding<span style="color: grey;">)</span> <span style="color: grey;">+</span> @InValue<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">IF</span> @PadSide <span style="color: grey;">=</span> <span style="color: red;">'R'</span> <span style="color: green;">-- Pad the right side<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Set</span> @Result
<span style="color: grey;">=</span> @InValue <span style="color: grey;">+</span> <span style="color: magenta;">Replicate</span><span style="color: grey;">(</span>@PadChar<span style="color: grey;">,</span> @Padding<span style="color: grey;">)</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">RETURN </span><span style="color: grey;">(</span>@Result<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">END</span><o:p></o:p></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"><br /></span></div>
<div class="MsoNormal">
</div>
<div>
The Above code can be used in both LEFT and RIGHT side padding just by passing appropriate parameter to the function.</div>
<div>
<br /></div>
<div>
Usage --</div>
<div>
<br /></div>
<div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> dbo<span style="color: grey;">.</span>fn_PadValue<span style="color: grey;">(</span><span style="color: red;">'171'</span><span style="color: grey;">,</span>6<span style="color: grey;">,</span><span style="color: red;">'0'</span><span style="color: grey;">,</span><span style="color: red;">'L'</span><span style="color: grey;">)</span> <span style="color: green;">--For Left Side Padding<o:p></o:p></span></span></div>
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-no-proof: yes;"> dbo<span style="color: grey;">.</span>fn_PadValue<span style="color: grey;">(</span><span style="color: red;">'171'</span><span style="color: grey;">,</span>6<span style="color: grey;">,</span><span style="color: red;">'0'</span><span style="color: grey;">,</span><span style="color: red;">'R'</span><span style="color: grey;">)</span> <span style="color: green;">--For Right
Side Padding</span></span></div>
<div>
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-ansi-language: EN-US; mso-bidi-language: AR-SA; mso-fareast-font-family: Calibri; mso-fareast-language: EN-US; mso-fareast-theme-font: minor-latin; mso-no-proof: yes;"><span style="color: green;"><br /></span></span></div>
<div>
<span style="color: green; font-family: Courier New; font-size: x-small;"><span style="line-height: 14px;"><br /></span></span></div>
<div>
<span style="color: green; font-family: Courier New; font-size: x-small;"><span style="line-height: 14px;">References -</span></span></div>
<div>
<span style="color: green; font-family: Courier New; font-size: x-small;"><span style="line-height: 14px;">http://www.sqlservercentral.com/scripts/T-SQL/64358/</span></span></div>
<div>
<span style="color: green; font-family: Courier New; font-size: x-small;"><span style="line-height: 14px;"><br /></span></span></div>
</div>
</div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-79237923818677268642013-03-21T11:24:00.000-07:002013-03-21T11:24:04.844-07:00Popular Date Time formats in SQL Server<div dir="ltr" style="text-align: left;" trbidi="on">
We usually come across scenario's where different date time conversions and formats are required in our day to day development activities.<br />
<br />
here are some useful format codes used in SQL Server -<br />
<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>100<span style="color: grey;">)</span> <span style="color: green;">--Apr 6 2011 4:38AM<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>101<span style="color: grey;">)</span> <span style="color: green;">--04/06/2011 (MM/DD/YYYY)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>102<span style="color: grey;">)</span> <span style="color: green;">--2011.04.06 (YYYY.MM.DD)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>103<span style="color: grey;">)</span> <span style="color: green;">--06/04/2011 (DD/MM/YYYY)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>104<span style="color: grey;">)</span> <span style="color: green;">--06.04.2011 (DD.MM.YYYY)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>105<span style="color: grey;">)</span> <span style="color: green;">--06-04-2011 (DD-MM-YYYY)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>106<span style="color: grey;">)</span> <span style="color: green;">--06 Apr 2011<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>107<span style="color: grey;">)</span> <span style="color: green;">--Apr 06, 2011<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>108<span style="color: grey;">)</span> <span style="color: green;">--04:42:19 (HH:MM:SS)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>109<span style="color: grey;">)</span> <span style="color: green;">--Apr 6 2011 4:42:47<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>110<span style="color: grey;">)</span> <span style="color: green;">--04-06-2011 (MM-DD-YYYY)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>111<span style="color: grey;">)</span> <span style="color: green;">--2011/04/06 (YYYY/MM/DD)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>112<span style="color: grey;">)</span> <span style="color: green;">--20110406 (YYYYMMDD)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>113<span style="color: grey;">)</span> <span style="color: green;">--06 Apr 2011
04:44:10<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>114<span style="color: grey;">)</span> <span style="color: green;">--04:44:25:583 (HH:MM:SS:sss)<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: magenta;">convert</span><span style="color: grey;">(</span><span style="color: blue;">varchar</span><span style="color: grey;">(</span>20<span style="color: grey;">),</span><span style="color: magenta;">GETDATE</span><span style="color: grey;">(),</span>120<span style="color: grey;">)</span> <span style="color: green;">--2011-04-06
04:45:20</span></span><o:p></o:p></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-26917346347997219912013-03-21T05:28:00.001-07:002013-03-21T05:28:15.364-07:00List all SQL Agent Jobs with atleast one SSIS Package used in it<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">Use</span><span style="font-family: 'Courier New'; font-size: 10pt;"> [msdb]</span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><br /></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> sysjobs<span style="color: grey;">.</span>name <span style="color: blue;">AS</span> <span style="color: red;">'Job Name'</span><span style="color: grey;">,</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">sysjobsteps<span style="color: grey;">.</span>step_id <span style="color: blue;">AS</span> <span style="color: red;">'Step Number'</span><span style="color: grey;">,</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">sysjobsteps<span style="color: grey;">.</span>command <span style="color: blue;">AS</span> <span style="color: red;">'SSIS Package Location'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> sysjobs </span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"><span style="color: grey;">INNER</span> <span style="color: grey;">JOIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">sysjobsteps <span style="color: blue;">ON</span> sysjobs<span style="color: grey;">.</span>job_id <span style="color: grey;">=</span> sysjobsteps<span style="color: grey;">.</span>job_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">where</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> subsystem<span style="color: grey;">=</span><span style="color: red;">'ssis'<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">order</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: blue;">by</span> 1</span><o:p></o:p></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-65380806133118429392013-03-21T00:11:00.002-07:002013-03-21T00:11:56.406-07:00Get the latest run duration of all jobs in SQL Server<div dir="ltr" style="text-align: left;" trbidi="on">
The Below Query helps to find the run duration of all jobs in a particular server --<br />
<br />
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> j<span style="color: grey;">.</span>name<span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> h<span style="color: grey;">.</span>run_status<span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> durationHHMMSS <span style="color: grey;">=</span>
<span style="color: magenta;">STUFF</span><span style="color: grey;">(</span><span style="color: magenta;">STUFF</span><span style="color: grey;">(</span><span style="color: magenta;">REPLACE</span><span style="color: grey;">(</span><span style="color: magenta;">STR</span><span style="color: grey;">(</span>h<span style="color: grey;">.</span>run_duration<span style="color: grey;">,</span>7<span style="color: grey;">,</span>0<span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: red;">' '</span><span style="color: grey;">,</span><span style="color: red;">'0'</span><span style="color: grey;">),</span>4<span style="color: grey;">,</span>0<span style="color: grey;">,</span><span style="color: red;">':'</span><span style="color: grey;">),</span>7<span style="color: grey;">,</span>0<span style="color: grey;">,</span><span style="color: red;">':'</span><span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> [start_date] <span style="color: grey;">=</span>
<span style="color: magenta;">CONVERT</span><span style="color: grey;">(</span><span style="color: blue;">DATETIME</span><span style="color: grey;">,</span> <span style="color: magenta;">RTRIM</span><span style="color: grey;">(</span>run_date<span style="color: grey;">)</span> <span style="color: grey;">+</span> <span style="color: red;">' '<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">+</span> <span style="color: magenta;">STUFF</span><span style="color: grey;">(</span><span style="color: magenta;">STUFF</span><span style="color: grey;">(</span><span style="color: magenta;">REPLACE</span><span style="color: grey;">(</span><span style="color: magenta;">STR</span><span style="color: grey;">(</span><span style="color: magenta;">RTRIM</span><span style="color: grey;">(</span>h<span style="color: grey;">.</span>run_time<span style="color: grey;">),</span>6<span style="color: grey;">,</span>0<span style="color: grey;">),<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: red;">' '</span><span style="color: grey;">,</span><span style="color: red;">'0'</span><span style="color: grey;">),</span>3<span style="color: grey;">,</span>0<span style="color: grey;">,</span><span style="color: red;">':'</span><span style="color: grey;">),</span>6<span style="color: grey;">,</span>0<span style="color: grey;">,</span><span style="color: red;">':'</span><span style="color: grey;">))<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> msdb<span style="color: grey;">.</span>dbo<span style="color: grey;">.</span>sysjobs <span style="color: blue;">AS</span> j<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> </span><span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">SELECT</span>
job_id<span style="color: grey;">,</span> instance_id <span style="color: grey;">=</span>
<span style="color: magenta;">MAX</span><span style="color: grey;">(</span>instance_id<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">FROM</span>
msdb<span style="color: grey;">.</span>dbo<span style="color: grey;">.</span>sysjobhistory<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">GROUP</span>
<span style="color: blue;">BY</span> job_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">)</span> <span style="color: blue;">AS</span> l<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ON</span> j<span style="color: grey;">.</span>job_id <span style="color: grey;">=</span> l<span style="color: grey;">.</span>job_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">INNER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">JOIN<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> msdb<span style="color: grey;">.</span>dbo<span style="color: grey;">.</span>sysjobhistory <span style="color: blue;">AS</span> h<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">ON</span> h<span style="color: grey;">.</span>job_id <span style="color: grey;">=</span> l<span style="color: grey;">.</span>job_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">AND</span> h<span style="color: grey;">.</span>instance_id <span style="color: grey;">=</span> l<span style="color: grey;">.</span>instance_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ORDER</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">BY<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">CONVERT</span><span style="color: grey;">(</span><span style="color: blue;">INT</span><span style="color: grey;">,</span> h<span style="color: grey;">.</span>run_duration<span style="color: grey;">)</span> <span style="color: blue;">DESC</span><span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">
[start_date] <span style="color: blue;">DESC</span></span><o:p></o:p></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-26010571371546752532013-03-11T06:28:00.001-07:002013-03-11T11:16:47.554-07:00SSIS vs. T-SQL<div dir="ltr" style="text-align: left;" trbidi="on">
This is a common question which comes often before any developer to choose between the two as to follow which path to choose as SSIS or T-SQL for developing ETL solutions.<br />
<br />
well i also got similar scenario some time back and was in a dilemma to decide this.<br />
<br />
on a big picture it's always better to go with conventional way i.e. using T-SQL and not to go with the tool.<br />
But on doing some more reading and research over many blogs and forums and also the development phase of my work over some 2 and a half odd years. I found some good reasons and explanations over this as not to always stick to a particular approach and do proper analysis of the requirements to take a decision.<br />
<br />
Below are some of the key points which i was able to collect regarding this.<br />
<br />
<span style="font-family: inherit;">1. <span style="line-height: 115%;">If the data does need to be
persisted temporarily it can be to a raw file in SSIS whereas T-SQL requires
temporary tables for which there may be a management/security overhead.</span></span><br />
<span style="font-family: Calibri, sans-serif; font-size: 10pt; line-height: 115%;"><br /></span>
<br />
<div style="text-align: left;">
<span style="font-family: Calibri, sans-serif;"><span style="line-height: 14px;">2. </span></span><span style="line-height: 115%; text-indent: -0.25in;">SSIS have a very good visual
representation of the "work".</span></div>
<div style="text-align: left;">
<span style="line-height: 115%; text-indent: -0.25in;"><br /></span></div>
<div style="text-align: left;">
<span style="line-height: 115%; text-indent: -0.25in;">3. </span><span style="line-height: 115%; text-indent: -0.25in;">SSIS manages memory very efficiently,
which can result in big performance improvements compared to T-SQL.</span></div>
<div style="text-align: left;">
<span style="line-height: 115%; text-indent: -0.25in;"><br /></span></div>
<div style="text-align: left;">
<span style="line-height: 115%; text-indent: -0.25in;">4. </span><span style="font-family: Calibri, sans-serif; line-height: 115%;">Bad data can be captured to a different data
sink for examination later in SSIS as it have an in-built debugger where we can
attach the data in between the transformations without using any temporary
object and can copy that data to excel file to analyze.</span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<span style="font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<span style="font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<span style="font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
<br />
5. <span style="line-height: 115%; text-indent: -0.25in;"><span style="line-height: normal;"> </span></span><span style="line-height: 115%; text-indent: -0.25in;">Exception Handling is better in SSIS
as compared to T-SQL.</span><br />
<span style="line-height: 115%; text-indent: -0.25in;"><br /></span>
<br />
<div style="text-indent: 0px;">
<span style="line-height: 115%; text-indent: -0.25in;">6. </span><span style="font-family: Calibri, sans-serif;"><span style="line-height: 115%;">Data-flows are, to some extent, self-documenting
so improves code </span><span style="line-height: 18px;">understand-ability</span><span style="line-height: 115%;"> as far as documentation is concerned.</span></span></div>
<div style="text-indent: 0px;">
<span style="font-family: Calibri, sans-serif;"><span style="line-height: 115%;"><br /></span></span></div>
<div style="text-indent: 0px;">
<span style="font-family: Calibri, sans-serif;"><span style="line-height: 115%;">7. </span></span><span style="line-height: 115%; text-indent: -0.25in;">User-controlled parallel execution of
data-flows is possible where it isn't really in the inherent batch operated
world of stored procedures.</span></div>
<div style="text-indent: 0px;">
<span style="line-height: 115%; text-indent: -0.25in;"><br /></span></div>
<div style="text-indent: 0px;">
<span style="line-height: 115%; text-indent: -0.25in;">8. </span><span style="line-height: 115%; text-indent: -0.25in;">"Heavy-lifting" of data can
occur on a different to machine to that storing the data.</span></div>
<span style="font-family: Calibri, sans-serif; line-height: 115%;">Regarding
the performance impact on the source server, since the Stored Proc's have to do
all the work on the server itself, while the data flow can be pulling data in </span><span style="font-family: Calibri, sans-serif; line-height: 115%;">an asynchronous way</span><span style="font-family: Calibri, sans-serif; line-height: 115%;"> and doing the heavy lifting on a different machine.</span><br />
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<span style="font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<span style="font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
<div>
<span style="font-family: Calibri, sans-serif; line-height: 115%;"><br /></span></div>
<div>
<span style="font-family: Calibri, sans-serif; line-height: 115%;">9. </span><span style="font-family: Calibri, sans-serif; line-height: 115%;">By default, events containing very
pertinent information such as "component "<component
name>" (5824)" wrote 2398156 rows" are rose which might be
very useful for logging and process tracking in case of any failures or logging
scenarios.</span></div>
<div>
<span style="font-family: Calibri, sans-serif; line-height: 115%;"><br /></span></div>
<div>
<span style="font-family: Calibri, sans-serif; line-height: 115%;">10. </span><b style="text-indent: -0.25in;"><span style="line-height: 115%;">For each Loop Container</span></b><span style="line-height: 115%; text-indent: -0.25in;"> in Integration Services takes a data type of <b>System.Object</b> (not the physical SQL
table), which is an in-memory object that is native to the Integration
Services, which is very effective in terms of performance optimization as
compared to Temp tables\tables variables\cursors.</span></div>
<div>
<span style="line-height: 115%; text-indent: -0.25in;"><br /></span></div>
<div>
<span style="line-height: 115%; text-indent: -0.25in;">Hope you find this post Useful.</span></div>
<div>
<span style="line-height: 115%; text-indent: -0.25in;"><br /></span></div>
<div>
<span style="text-indent: -0.25in;"><span style="line-height: 115%;">Do comment if it you need any further information on this or if you find anything here which </span><span style="line-height: 18px;">doesn't looks convincing.</span></span></div>
<div>
<span style="text-indent: -0.25in;"><span style="line-height: 18px;"><br /></span></span></div>
<div>
<span style="text-indent: -0.25in;"><span style="line-height: 18px;">Happy Learning !!</span></span></div>
<div class="MsoListParagraph" style="mso-list: l0 level1 lfo1; text-indent: -.25in;">
<span style="font-size: 10.0pt; line-height: 115%;"><o:p></o:p></span></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com2tag:blogger.com,1999:blog-35883176724266705.post-54140317711873668452013-01-30T02:26:00.003-08:002013-01-30T02:26:20.901-08:00SQL Query to list all tables with Identity columns<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> t<span style="color: grey;">.</span>name <span style="color: blue;">AS</span> [Table_Name]<span style="color: grey;">,</span> c<span style="color: grey;">.</span>name <span style="color: blue;">AS</span> [Column_Name]<span style="color: grey;">,<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">tp<span style="color: grey;">.</span>name
<span style="color: blue;">AS</span> [Data_Type]<span style="color: grey;">,</span>c<span style="color: grey;">.</span>max_length <span style="color: blue;">AS</span> [Size]<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">tables</span> t <span style="color: grey;">JOIN</span> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">columns</span> c <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">ON</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> t<span style="color: grey;">.</span><span style="color: magenta;">object_id</span> <span style="color: grey;">=</span> c<span style="color: grey;">.</span><span style="color: magenta;">object_id<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">JOIN</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">sys</span><span style="color: grey;">.</span><span style="color: green;">types</span> tp <span style="color: blue;">ON</span> c<span style="color: grey;">.</span>system_type_id <span style="color: grey;">=</span> tp<span style="color: grey;">.</span>system_type_id<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> c<span style="color: grey;">.</span>is_identity <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">and</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> t<span style="color: grey;">.</span>name <span style="color: grey;">not</span> <span style="color: grey;">like</span> <span style="color: red;">'t_%'<o:p></o:p></span></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">order</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> <span style="color: blue;">by</span> t<span style="color: grey;">.</span>name</span></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-11320458277750876882013-01-04T01:03:00.002-08:002013-01-04T01:03:54.780-08:00How to Delete Duplicates from a table with no Identity Column<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">with</span><span style="font-family: 'Courier New'; font-size: 10pt;"> duplicate <span style="color: blue;">as </span><span style="color: grey;">(</span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> row1<span style="color: grey;">,</span>row2<span style="color: grey;">,</span>row3<span style="color: grey;">,</span> <o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: magenta; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">row_Number</span><span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
</span><span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">()</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">Over </span><span style="color: grey;">(</span><span style="color: blue;">partition</span> <span style="color: blue;">by</span> row1<span style="color: grey;">,</span>row2<span style="color: grey;">,</span>row3 <span style="color: blue;">order</span> <span style="color: blue;">by</span> row1<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">as</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> RowNumber<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> YourTable<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">delete</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">from</span> duplicate<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">where</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;"> RowNumber <span style="color: grey;">>=</span>2</span></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-57376349585242976552012-12-13T04:28:00.002-08:002012-12-13T04:28:13.826-08:00Storing Column Names of a table in CSV Format<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@commaSeperatedCols <span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span><span style="color: magenta;">MAX</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DECLARE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @TableName
<span style="color: blue;">VARCHAR</span><span style="color: grey;">(</span>100<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SET</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @TableName
<span style="color: grey;">=</span> <span style="color: red;">'Your_Table_Name'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">
@commaSeperatedCols <span style="color: grey;">=</span> <span style="color: magenta;">COALESCE</span><span style="color: grey;">(</span>@commaSeperatedCols<span style="color: grey;">+</span><span style="color: red;">','</span> <span style="color: grey;">,</span><span style="color: red;">''</span><span style="color: grey;">)</span> <span style="color: grey;">+</span> COLUMN_NAME<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: green;">INFORMATION_SCHEMA</span><span style="color: grey;">.</span><span style="color: green;">COLUMNS<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> TABLE_NAME
<span style="color: grey;">=</span> @TableName<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<br /></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">
@commaSeperatedCols</span></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-81274014854873350922012-12-03T04:11:00.003-08:002012-12-03T04:11:54.600-08:00Tricky Query to find the First lower case character in a String in SQL Server<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">Declare</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @i <span style="color: blue;">int</span> <span style="color: grey;">,</span> @loop <span style="color: blue;">int</span> <span style="color: grey;">,</span> @str <span style="color: blue;">varchar</span><span style="color: grey;">(</span>100<span style="color: grey;">)</span> <span style="color: grey;">=</span> <span style="color: red;">'TEST_lOwerCase'<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">IF</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">EXISTS<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">SELECT</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: grey;">*<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">FROM</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> tempdb<span style="color: grey;">.</span>dbo<span style="color: grey;">.</span><span style="color: green;">sysobjects<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">WHERE</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> ID <span style="color: grey;">=</span> <span style="color: magenta;">OBJECT_ID</span><span style="color: grey;">(</span><span style="color: red;">N'tempdb..#Temp'</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">)<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">DROP</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">TABLE</span> #Temp<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">create</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">table</span> #Temp<span style="color: blue;"> </span><span style="color: grey;">(</span>id <span style="color: blue;">int</span><span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @i <span style="color: grey;">=</span> <span style="color: magenta;">LEN</span><span style="color: grey;">(</span>@str<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">print</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @i<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">set</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> @loop <span style="color: grey;">=</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">while</span><span style="color: grey; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">(</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">@loop <span style="color: grey;"><=</span> @i<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">BEGIN<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">if</span><span style="color: grey;">((</span><span style="color: magenta;">ascii</span><span style="color: grey;">(</span><span style="color: magenta;">SUBSTRING</span><span style="color: grey;">(</span>@str<span style="color: grey;">,</span>@loop<span style="color: grey;">,</span>1<span style="color: grey;">))>=</span>97<span style="color: grey;">)</span> <span style="color: grey;">and</span><span style="color: blue;"> </span><span style="color: grey;">(</span><span style="color: magenta;">ascii</span><span style="color: grey;">(</span><span style="color: magenta;">SUBSTRING</span><span style="color: grey;">(</span>@str<span style="color: grey;">,</span>@loop<span style="color: grey;">,</span>1<span style="color: grey;">))<=</span>122<span style="color: grey;">))<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">insert</span> <span style="color: blue;">into</span>
#Temp <span style="color: blue;">values </span><span style="color: grey;">(</span>@loop<span style="color: grey;">)<o:p></o:p></span></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: blue;">set</span> @loop <span style="color: grey;">=</span> @loop <span style="color: grey;">+</span> 1<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">END<o:p></o:p></span></div>
<div class="MsoNormal" style="margin-bottom: 0.0001pt;">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;">select</span><span style="font-family: "Courier New"; font-size: 10.0pt; mso-no-proof: yes;"> <span style="color: magenta;">MIN</span><span style="color: grey;">(</span>id<span style="color: grey;">)</span> <span style="color: blue;">from</span> #Temp<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="color: blue; font-family: "Courier New"; font-size: 10.0pt; line-height: 115%; mso-no-proof: yes;">GO</span></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-80198968216641072312012-09-29T23:54:00.002-07:002012-10-01T22:53:47.755-07:00Adding AutoComplete Functionality to a TextBox Control using C#.Net <div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; font-size: 10.0pt;"><br />
</span><span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">Hello Friends !!<o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">Today I am writing my first C#.Net Article going a bit out of my
way from SQL and MSBI.<o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">This is something related to a requirement which comes my way
when in a windows Application i need to give a drop down for all like customer
names when typing the names in the Text Box as a very basic user friendly
option.<o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">Here is how we can do this -<o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">Let's take as assumption as we have a table in SQL as
CustomerTable and we want to take the FirstName of the Customer as our search
result.<o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">we will write this code in the Form load event of the form which
contains the Text box.<o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">The connectionStringName will be configured and can be get from
the App.Config table.<o:p></o:p></span><br />
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";"><br /></span>
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">Also you need to change the below 2 properties of the Text Box on which you want to implement the Auto Complete functionality.</span><br />
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";"><br /></span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj437CeviUt8rpaPf2skjxPh3DOtt-iALOiSkENoHxjoeRhMZTXFkZXu15F_KZErZCuRTHhX1hQgCYiLTON79AzmeA5qzEYZejDK75ioCi8kgyf1dNH2AL0dGj5vAfKTAs1zDjdqvZOgeo/s1600/properties.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj437CeviUt8rpaPf2skjxPh3DOtt-iALOiSkENoHxjoeRhMZTXFkZXu15F_KZErZCuRTHhX1hQgCYiLTON79AzmeA5qzEYZejDK75ioCi8kgyf1dNH2AL0dGj5vAfKTAs1zDjdqvZOgeo/s1600/properties.png" /></a></div>
<span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">SqlConnection</span><span style="color: #333333; font-family: "Segoe UI","sans-serif"; font-size: 10pt;"> con
= new </span><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">SqlConnection</span><span style="color: #333333; font-family: "Segoe UI","sans-serif"; font-size: 10pt;">(</span><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10pt;">ConfigurationManager</span><span style="color: #333333; font-family: "Segoe UI","sans-serif"; font-size: 10pt;">.ConnectionStrings[</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10pt;">"connectionStringName"</span><span color:="color:" segoe="" serif="serif" style="font-family: "; font-size: 10pt;" ui="ui">].ConnectionString);</span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">SqlCommand</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";"> cmd
= </span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">new</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";"> </span><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">SqlCommand</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">(</span><span style="color: #a31515; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">"SELECT
FirstName FROM CustomerTable"</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">,
con);<br style="outline: none 0px;" />
con.Open();<br style="outline: none 0px;" />
</span><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">SqlDataReader</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";"> reader = cmd.ExecuteReader();<br style="outline: none 0px;" />
</span><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">AutoCompleteStringCollection</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";"> MyCollection = </span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">new</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";"> </span><span style="color: #2b91af; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">AutoCompleteStringCollection</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">();<br style="outline: none 0px;" />
</span><span style="color: blue; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";">while</span><span style="color: #333333; font-family: "Verdana","sans-serif"; font-size: 10.0pt; mso-bidi-font-family: "Segoe UI";"> (reader.Read())<br style="outline: none 0px;" />
{<br style="outline: none 0px;" />
MyCollection.Add(reader.GetString(0));<br style="outline: none 0px;" />
}<br style="outline: none 0px;" />
txtFirstName.AutoCompleteCustomSource = MyCollection;<br style="outline: none 0px;" />
con.Close();</span><span style="color: #333333; font-family: "Segoe UI","sans-serif";"><o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">This will automatically get refreshed as when we loads the form
after any changes in the DB.</span><span style="color: #333333; font-family: "Segoe UI","sans-serif";"><o:p></o:p></span></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<br /></div>
<div style="background: white; margin-bottom: .0001pt; margin: 0cm;">
<span style="color: orange; font-family: "Verdana","sans-serif"; mso-bidi-font-family: "Segoe UI";">Happy Coding !!</span><span style="color: #333333; font-family: "Segoe UI","sans-serif";"><o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
</div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0tag:blogger.com,1999:blog-35883176724266705.post-10377502167739391502012-09-17T02:17:00.002-07:002012-09-30T10:44:11.392-07:00Pump data from one server to another Efficiently<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<br />
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">Sometimes the requirement
comes when we need to pump the data from one server to another server.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">Though we have SQL
Import\Export wizard which can do this quiet easily, but in that way we will
loose some important features of the table --</span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"> 1.
The Identity property of a column in case we have one in the table.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"> 2.
All the indexes.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br /></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">To avoid this we can go
with a simple SSIS package to which we just need to pass the table name as a
parameter and it will do the rest, taking care of the above constraints.</span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br />
This is how the Package looks -<o:p></o:p></span></div>
</div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp5OaStyfJCMdU9blQ-qBpIDWf72pdTRld4HJ0ult3muztR5z9nlqqOhNGr2Vi1tdHXDvLhIks3Aez8rjUc0lViiEZ5A0QFcBvv04dwfTTKmAA8qjuwyS6iKBaXGNCKBfyQl_9XkIGrg8/s1600/SSIS-DataPull.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhp5OaStyfJCMdU9blQ-qBpIDWf72pdTRld4HJ0ult3muztR5z9nlqqOhNGr2Vi1tdHXDvLhIks3Aez8rjUc0lViiEZ5A0QFcBvv04dwfTTKmAA8qjuwyS6iKBaXGNCKBfyQl_9XkIGrg8/s640/SSIS-DataPull.jpg" width="557" /></a></div>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span></div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span><br />
<br />
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">Steps -<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br />
1. To make the executable dynamic we will drive the package from a table, where
we will store the names of all the tables which we want to refresh
from the source server and we will store it in a result set variable
(Table_List)<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br />
Refer the variables list in the below figure used in the package.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br /></span></div>
</div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK2AVS9gkEPMsdqU2XxaJgkhfQXmzueF-W6sNZS7RYyAD3fqpHTFwcUIqBxox__HQxrqdL5EwsX5L-xjjlMT6tsNB3poe9mpX8-cVntawY918M4zAystDTtnJmoFPshhC5Cczl1jN4mQA/s1600/variables.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="430" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjK2AVS9gkEPMsdqU2XxaJgkhfQXmzueF-W6sNZS7RYyAD3fqpHTFwcUIqBxox__HQxrqdL5EwsX5L-xjjlMT6tsNB3poe9mpX8-cVntawY918M4zAystDTtnJmoFPshhC5Cczl1jN4mQA/s640/variables.jpg" width="640" /></a></div>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span><br />
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">2. Create a For each loop
container which will iterate through each item in the result stored in the
Table_List variable.<o:p></o:p></span></div>
<br />
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLgBAPrf8bxTGvYRc4ZgogxOvPKDt_m2Qh2u-5HY63h-IJtpZS8zOBzm5QD2goVy3frxSfPEVsHEU4QazfE7doGUon-tzT5NioQ7_DJl82YWl54_yeaHSnZSjLtFLq9Qx34XSyIglB3bo/s1600/container.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="386" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjLgBAPrf8bxTGvYRc4ZgogxOvPKDt_m2Qh2u-5HY63h-IJtpZS8zOBzm5QD2goVy3frxSfPEVsHEU4QazfE7doGUon-tzT5NioQ7_DJl82YWl54_yeaHSnZSjLtFLq9Qx34XSyIglB3bo/s640/container.png" width="640" /></a></div>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span><br />
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">3. Assign the value from
the result set to the variable</span><span style="color: orange; font-family: "Verdana","sans-serif";"> </span><b><span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">TableName</span></b><span style="color: orange; font-family: "Verdana","sans-serif";"> </span><span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">for each iteration.<o:p></o:p></span></div>
<br />
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_SdgflON3kb-VkHTSjFDyz_BXj59uL6xLw9tKZ61yhx1A_OOriz5p6aRH1xv5wLK7ypCnXGTEZwHrvG06rPsjqsGwqtgfChr3ifW5wl_-KzbehFqrGHYfY2gfneFbUr6OKcAkf7OuCDE/s1600/container2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="222" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi_SdgflON3kb-VkHTSjFDyz_BXj59uL6xLw9tKZ61yhx1A_OOriz5p6aRH1xv5wLK7ypCnXGTEZwHrvG06rPsjqsGwqtgfChr3ifW5wl_-KzbehFqrGHYfY2gfneFbUr6OKcAkf7OuCDE/s640/container2.png" width="640" /></a></div>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span>
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span><br />
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">4. Now inside the loop
configure each of the execute SQL task as below -<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br />
5. Check the existence of the table in the destination server.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br />
6. If the table is not there, Use a Execute SQL Task which creates the
query of create table from the source server and stores it in a variable.<o:p></o:p></span></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;"><br />
Below is the query which will give you the DDL for a creating the table in
destination from the server.<o:p></o:p></span></div>
</div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<span style="font-family: Georgia, Times New Roman, serif;"><b><br /></b></span></div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
</div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt;"> @vsSQL <span style="color: blue;">varchar</span><span style="color: grey;">(</span>8000<span style="color: grey;">)<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">declare</span><span style="font-family: 'Courier New'; font-size: 10pt;"> @vsTableName <span style="color: blue;">varchar</span><span style="color: grey;">(</span>50<span style="color: grey;">)<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">set</span><span style="font-family: 'Courier New'; font-size: 10pt;"> @vsTableName <span style="color: grey;">=</span> <span style="color: red;">'Your Table Name'<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<br /></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">select</span><span style="font-family: 'Courier New'; font-size: 10pt;"> @vsSQL <span style="color: grey;">=</span> <span style="color: red;">'CREATE TABLE '</span> <span style="color: grey;">+</span> @vsTableName <span style="color: grey;">+</span> <span style="color: blue;">char</span><span style="color: grey;">(</span>10<span style="color: grey;">)</span> <span style="color: grey;">+</span> <span style="color: red;">'('</span> <span style="color: grey;">+</span> <span style="color: blue;">char</span><span style="color: grey;">(</span>10<span style="color: grey;">)<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<br /></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">select</span><span style="font-family: 'Courier New'; font-size: 10pt;"> @vsSQL <span style="color: grey;">=</span> @vsSQL <span style="color: grey;">+</span> <span style="color: red;">' '</span> <span style="color: grey;">+</span> sc<span style="color: grey;">.</span>Name <span style="color: grey;">+</span> <span style="color: red;">' '</span> <span style="color: grey;">+<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="font-family: 'Courier New'; font-size: 10pt;">st<span style="color: grey;">.</span>Name <span style="color: grey;">+<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">case</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: blue;">when</span> st<span style="color: grey;">.</span>Name <span style="color: grey;">in</span><span style="color: blue;"> </span><span style="color: grey;">(</span><span style="color: red;">'varchar'</span><span style="color: grey;">,</span><span style="color: red;">'varchar'</span><span style="color: grey;">,</span><span style="color: red;">'char'</span><span style="color: grey;">,</span><span style="color: red;">'<wbr></wbr>nchar'</span><span style="color: grey;">)</span> <span style="color: blue;">then</span> <span style="color: red;">'('</span> <span style="color: grey;">+</span> <span style="color: magenta;">cast</span><span style="color: grey;">(</span>sc<span style="color: grey;">.</span><span style="color: blue;">Length</span> <span style="color: blue;">as</span> <span style="color: blue;">varchar</span><span style="color: grey;">)</span> <span style="color: grey;">+</span><span style="color: red;">') '</span> <span style="color: blue;">else</span> <span style="color: red;">' '</span> <span style="color: blue;">end</span> <span style="color: grey;">+<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">case</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: blue;">when</span> sc<span style="color: grey;">.</span>IsNullable <span style="color: grey;">=</span> 1 <span style="color: blue;">then</span> <span style="color: red;">'NULL'</span> <span style="color: blue;">else</span> <span style="color: red;">'NOT NULL'</span> <span style="color: blue;">end</span> <span style="color: grey;">+</span> <span style="color: red;">','</span> <span style="color: grey;">+</span> <span style="color: blue;">char</span><span style="color: grey;">(</span>10<span style="color: grey;">)<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">from</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: green;">sysobjects</span> so<u></u><u></u></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: grey; font-family: 'Courier New'; font-size: 10pt;">join</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: green;">syscolumns</span> sc <span style="color: blue;">on</span> sc<span style="color: grey;">.</span>id <span style="color: grey;">=</span> so<span style="color: grey;">.</span>id<u></u><u></u></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: grey; font-family: 'Courier New'; font-size: 10pt;">join</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: green;">systypes</span> st <span style="color: blue;">on</span> st<span style="color: grey;">.</span>xusertype <span style="color: grey;">=</span> sc<span style="color: grey;">.</span>xusertype<u></u><u></u></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">where</span><span style="font-family: 'Courier New'; font-size: 10pt;"> so<span style="color: grey;">.</span>name <span style="color: grey;">=</span> @vsTableName<u></u><u></u></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">order</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: blue;">by<u></u><u></u></span></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="font-family: 'Courier New'; font-size: 10pt;">sc<span style="color: grey;">.</span>ColID<u></u><u></u></span></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<br /></div>
<div class="MsoNormal" style="color: #222222; font-family: arial, sans-serif;">
<span style="color: blue; font-family: 'Courier New'; font-size: 10pt;">select</span><span style="font-family: 'Courier New'; font-size: 10pt;"> <span style="color: magenta;">substring</span><span style="color: grey;">(</span>@vsSQL<span style="color: grey;">,</span>1<span style="color: grey;">,</span><span style="color: magenta;">len</span><span style="color: grey;">(</span>@vsSQL<span style="color: grey;">)</span> <span style="color: grey;">-</span> 2<span style="color: grey;">)</span> <span style="color: grey;">+</span> <span style="color: blue;">char</span><span style="color: grey;">(</span>10<span style="color: grey;">)</span> <span style="color: grey;">+</span> <span style="color: red;">')'</span></span></div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<br /></div>
<div class="MsoNormal" style="background-color: white; font-size: 13px; text-align: left;">
<br />
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">7. Now use another
Execute SQL Task to dump the data from Source to Destination.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">8. you can now extract
the index scripts on the table easily from the source server and execute them
into the Destination server in another Execute SQL Task at the end.<o:p></o:p></span></div>
<div class="MsoNormal">
<br /></div>
<div class="MsoNormal">
<span style="background-position: initial initial; background-repeat: initial initial; color: orange; font-family: Verdana, sans-serif; font-size: 10pt; line-height: 115%;">Thanks for Reading !!
Hope it helped you.<o:p></o:p></span></div>
</div>
<br /></div>
Ashish Agarwalhttp://www.blogger.com/profile/04774609573330867773noreply@blogger.com0