[{"data":1,"prerenderedAt":2926},["ShallowReactive",2],{"tag-sql":3},{"tag":4,"articles":24},{"id":5,"title":6,"body":7,"description":14,"extension":15,"img":16,"meta":17,"name":18,"navigation":19,"path":20,"seo":21,"stem":22,"__hash__":23},"tags\u002Ftags\u002Fsql.md","Sql",{"type":8,"value":9,"toc":10},"minimark",[],{"title":11,"searchDepth":12,"depth":12,"links":13},"",2,[],"SQL is a standard language designed for managing data in relational database management system. SQL stands for Structured Query Language. SQL is a standard programming language specifically designed for storing, retrieving, managing or manipulating the data inside a relational database management system (RDBMS).","md","https:\u002F\u002Fimages.unsplash.com\u002Fphoto-1598313183973-4effcded8d5e?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=675&q=80",{},"sql",true,"\u002Ftags\u002Fsql",{"description":14},"tags\u002Fsql","HleFpAIKGUPMxp855dHtmfuv32MNyIbaOTi0ZjW_I1k",[25,332,481,517,556,622,766,1061,1118,2127,2417,2859],{"id":26,"title":27,"author":28,"body":29,"createdAt":323,"description":324,"extension":15,"img":325,"meta":326,"navigation":19,"path":327,"seo":328,"stem":329,"tags":330,"updatedAt":323,"__hash__":331},"articles\u002Farticles\u002Frename-sql-server-schema.md","Rename SQL Server Schema","[object Object]",{"type":8,"value":30,"toc":321},[31,35,38,317],[32,33,34],"p",{},"Copying SQL Databases, renaming is not as easy as it could be.  Luckily the following script can make the effort much easier.  Give it a try.",[32,36,37],{},"After copying a SQL Server database I needed to create a new schema and transfer all tables and views and stored procedures\nfrom the old schema to the newly created schema.  Using the following script made this task much easier.",[39,40,43],"pre",{"className":41,"code":42,"language":18,"meta":11,"style":11},"language-sql shiki shiki-themes github-light github-dark","DECLARE @OldSchema AS varchar(255)\nDECLARE @NewSchema AS varchar(255)\n\nSET @OldSchema = 'dbo'\nSET @NewSchema = 'StackOverflow'\n\nDECLARE @sql AS varchar(MAX)\n\nDECLARE @Schema AS varchar(MAX)\nDECLARE @Obj AS varchar(MAX)\n\n-- First transfer Tables and Views\n\nDECLARE CU_OBJS CURSOR FOR\nSELECT TABLE_SCHEMA, TABLE_NAME\nFROM INFORMATION_SCHEMA.TABLES\nWHERE TABLE_SCHEMA = @OldSchema\n\nOPEN CU_OBJS\n\nFETCH NEXT FROM CU_OBJS\nINTO @Schema, @Obj\n\nWHILE @@FETCH_STATUS = 0\nBEGIN\nSELECT @sql = 'ALTER SCHEMA [' + @NewSchema + '] TRANSFER [' + @OldSchema + '].[' + @Obj + ']'\nPRINT @sql\n--  EXEC (@sql)\n\n    FETCH NEXT FROM CU_OBJS\n    INTO @Schema, @Obj\nEND\n\nCLOSE CU_OBJS\nDEALLOCATE CU_OBJS\n\n\n-- Now transfer Stored Procedures\n\nDECLARE CU_OBJS CURSOR FOR\nSELECT sys.schemas.name, sys.procedures.name\nFROM sys.procedures,sys.schemas\nWHERE sys.procedures.schema_id=sys.schemas.schema_id and sys.schemas.name = @OldSchema\n\nOPEN CU_OBJS\n\nFETCH NEXT FROM CU_OBJS\nINTO @Schema, @Obj\n",[44,45,46,54,59,65,71,77,82,88,93,99,105,110,116,121,127,133,139,145,150,156,161,167,173,178,184,190,196,202,208,213,219,225,231,236,242,248,253,258,264,269,274,280,286,292,297,302,307,312],"code",{"__ignoreMap":11},[47,48,51],"span",{"class":49,"line":50},"line",1,[47,52,53],{},"DECLARE @OldSchema AS varchar(255)\n",[47,55,56],{"class":49,"line":12},[47,57,58],{},"DECLARE @NewSchema AS varchar(255)\n",[47,60,62],{"class":49,"line":61},3,[47,63,64],{"emptyLinePlaceholder":19},"\n",[47,66,68],{"class":49,"line":67},4,[47,69,70],{},"SET @OldSchema = 'dbo'\n",[47,72,74],{"class":49,"line":73},5,[47,75,76],{},"SET @NewSchema = 'StackOverflow'\n",[47,78,80],{"class":49,"line":79},6,[47,81,64],{"emptyLinePlaceholder":19},[47,83,85],{"class":49,"line":84},7,[47,86,87],{},"DECLARE @sql AS varchar(MAX)\n",[47,89,91],{"class":49,"line":90},8,[47,92,64],{"emptyLinePlaceholder":19},[47,94,96],{"class":49,"line":95},9,[47,97,98],{},"DECLARE @Schema AS varchar(MAX)\n",[47,100,102],{"class":49,"line":101},10,[47,103,104],{},"DECLARE @Obj AS varchar(MAX)\n",[47,106,108],{"class":49,"line":107},11,[47,109,64],{"emptyLinePlaceholder":19},[47,111,113],{"class":49,"line":112},12,[47,114,115],{},"-- First transfer Tables and Views\n",[47,117,119],{"class":49,"line":118},13,[47,120,64],{"emptyLinePlaceholder":19},[47,122,124],{"class":49,"line":123},14,[47,125,126],{},"DECLARE CU_OBJS CURSOR FOR\n",[47,128,130],{"class":49,"line":129},15,[47,131,132],{},"SELECT TABLE_SCHEMA, TABLE_NAME\n",[47,134,136],{"class":49,"line":135},16,[47,137,138],{},"FROM INFORMATION_SCHEMA.TABLES\n",[47,140,142],{"class":49,"line":141},17,[47,143,144],{},"WHERE TABLE_SCHEMA = @OldSchema\n",[47,146,148],{"class":49,"line":147},18,[47,149,64],{"emptyLinePlaceholder":19},[47,151,153],{"class":49,"line":152},19,[47,154,155],{},"OPEN CU_OBJS\n",[47,157,159],{"class":49,"line":158},20,[47,160,64],{"emptyLinePlaceholder":19},[47,162,164],{"class":49,"line":163},21,[47,165,166],{},"FETCH NEXT FROM CU_OBJS\n",[47,168,170],{"class":49,"line":169},22,[47,171,172],{},"INTO @Schema, @Obj\n",[47,174,176],{"class":49,"line":175},23,[47,177,64],{"emptyLinePlaceholder":19},[47,179,181],{"class":49,"line":180},24,[47,182,183],{},"WHILE @@FETCH_STATUS = 0\n",[47,185,187],{"class":49,"line":186},25,[47,188,189],{},"BEGIN\n",[47,191,193],{"class":49,"line":192},26,[47,194,195],{},"SELECT @sql = 'ALTER SCHEMA [' + @NewSchema + '] TRANSFER [' + @OldSchema + '].[' + @Obj + ']'\n",[47,197,199],{"class":49,"line":198},27,[47,200,201],{},"PRINT @sql\n",[47,203,205],{"class":49,"line":204},28,[47,206,207],{},"--  EXEC (@sql)\n",[47,209,211],{"class":49,"line":210},29,[47,212,64],{"emptyLinePlaceholder":19},[47,214,216],{"class":49,"line":215},30,[47,217,218],{},"    FETCH NEXT FROM CU_OBJS\n",[47,220,222],{"class":49,"line":221},31,[47,223,224],{},"    INTO @Schema, @Obj\n",[47,226,228],{"class":49,"line":227},32,[47,229,230],{},"END\n",[47,232,234],{"class":49,"line":233},33,[47,235,64],{"emptyLinePlaceholder":19},[47,237,239],{"class":49,"line":238},34,[47,240,241],{},"CLOSE CU_OBJS\n",[47,243,245],{"class":49,"line":244},35,[47,246,247],{},"DEALLOCATE CU_OBJS\n",[47,249,251],{"class":49,"line":250},36,[47,252,64],{"emptyLinePlaceholder":19},[47,254,256],{"class":49,"line":255},37,[47,257,64],{"emptyLinePlaceholder":19},[47,259,261],{"class":49,"line":260},38,[47,262,263],{},"-- Now transfer Stored Procedures\n",[47,265,267],{"class":49,"line":266},39,[47,268,64],{"emptyLinePlaceholder":19},[47,270,272],{"class":49,"line":271},40,[47,273,126],{},[47,275,277],{"class":49,"line":276},41,[47,278,279],{},"SELECT sys.schemas.name, sys.procedures.name\n",[47,281,283],{"class":49,"line":282},42,[47,284,285],{},"FROM sys.procedures,sys.schemas\n",[47,287,289],{"class":49,"line":288},43,[47,290,291],{},"WHERE sys.procedures.schema_id=sys.schemas.schema_id and sys.schemas.name = @OldSchema\n",[47,293,295],{"class":49,"line":294},44,[47,296,64],{"emptyLinePlaceholder":19},[47,298,300],{"class":49,"line":299},45,[47,301,155],{},[47,303,305],{"class":49,"line":304},46,[47,306,64],{"emptyLinePlaceholder":19},[47,308,310],{"class":49,"line":309},47,[47,311,166],{},[47,313,315],{"class":49,"line":314},48,[47,316,172],{},[318,319,320],"style",{},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":11,"searchDepth":12,"depth":12,"links":322},[],"2020-10-13T13:50:59.878Z","Copying SQL Databases, renaming is not as easy as it could be.  Luckily the following script can make the effort much easier.  Give it a try. After copying a SQL Server database I needed to create a new schema and transfer all tables and views and stored procedures from the old schema to the newly created schema.  Using the following script made this task much easier.","\u002Farticles\u002Fimages\u002Fsingleton_thumb.png",{},"\u002Farticles\u002Frename-sql-server-schema",{"title":27,"description":324},"articles\u002Frename-sql-server-schema",[18],"mvDlpJpU00m0MVu_Frr5AdZnQYdcoEq59ox8SmjvJgs",{"id":333,"title":334,"author":335,"body":336,"createdAt":471,"description":472,"extension":15,"img":473,"meta":474,"navigation":19,"path":475,"seo":476,"stem":477,"tags":478,"updatedAt":471,"__hash__":480},"articles\u002Farticles\u002Fstring-comparisons.md","String Comparisons",null,{"type":8,"value":337,"toc":469},[338,341,346,354,358,365,369,375,379,385,389,395,399,409,412,425,455,467],[32,339,340],{},"In .NET there are 6 ways to compare strings.  Really? Why is it so difficult?",[342,343,345],"h4",{"id":344},"ordinal","Ordinal",[32,347,348,349,353],{},"Performs a simple byte comparison that is independent of language. This is most appropriate when comparing strings that are generated programmatically or ",[350,351,352],"strong",{},"when comparing case-sensitive resources"," such as passwords.",[342,355,357],{"id":356},"ordinalignorecase","OrdinalIgnoreCase",[32,359,360,361,364],{},"Treats the characters in the strings to compare as if they were converted to uppercase using the conventions of the invariant culture, and then performs a simple byte comparison that is independent of language. This is most appropriate when comparing strings that are generated programmatically or when",[350,362,363],{}," comparing case-insensitive resources such as paths and filenames",".",[342,366,368],{"id":367},"invariantculture","InvariantCulture",[32,370,371,372,364],{},"Compares strings in a linguistically relevant manner, but it is not suitable for display in any particular culture. Its",[350,373,374],{}," major application is to order strings in a way that will be identical across cultures",[342,376,378],{"id":377},"invariantcultureignorecase","InvariantCultureIgnoreCase",[32,380,381,382,364],{},"Compares strings in a linguistically relevant manner that ignores case, but it is not suitable for display in any particular culture. Its major application is to ",[350,383,384],{},"order strings in a way that will be identical across cultures",[342,386,388],{"id":387},"currentculture","CurrentCulture",[32,390,391,392],{},"Can be used when strings are linguistically relevant. For example, if strings are displayed to the user, or if strings are the result of user interaction,",[350,393,394],{}," culture-sensitive string comparison should be used to order the string data.",[342,396,398],{"id":397},"currentcultureignorecase","CurrentCultureIgnoreCase",[32,400,401,402,364,405,408],{},"Can be used when strings are linguistically relevant but their case is not. For example, if strings are displayed to the user but case is unimportant, culture-sensitive, ",[350,403,404],{},"case-insensitive string comparison should be used to order the string data",[406,407],"br",{},"\nTip: You should always specify explicitly the comparer as the default value is not consistent.",[32,410,411],{},"For instance,",[32,413,414,417,418,421,422,424],{},[44,415,416],{},"string.IndexOf"," uses the current culture whereas ",[44,419,420],{},"string.Equals"," uses Ordinal.",[406,423],{},"\ni.e.",[39,426,430],{"className":427,"code":428,"language":429,"meta":11,"style":11},"language-cs shiki shiki-themes github-light github-dark","string.Equals(\"\", \"\", StringComparison.Ordinal); \n\nnew [] { \"\" }.Contains(\"\", StringComparer.Ordinal); \n\nnew Dictionary(StringComparer.Ordinal); \n\n","cs",[44,431,432,437,441,446,450],{"__ignoreMap":11},[47,433,434],{"class":49,"line":50},[47,435,436],{},"string.Equals(\"\", \"\", StringComparison.Ordinal); \n",[47,438,439],{"class":49,"line":12},[47,440,64],{"emptyLinePlaceholder":19},[47,442,443],{"class":49,"line":61},[47,444,445],{},"new [] { \"\" }.Contains(\"\", StringComparer.Ordinal); \n",[47,447,448],{"class":49,"line":67},[47,449,64],{"emptyLinePlaceholder":19},[47,451,452],{"class":49,"line":73},[47,453,454],{},"new Dictionary(StringComparer.Ordinal);\n",[32,456,457,458,466],{},"Refer to ",[459,460,465],"a",{"href":461,"target":462,"rel":463},"https:\u002F\u002Fwww.meziantou.net\u002Fstring-comparisons-are-harder-than-it-seems.htm","_blank",[464],"noopener","blog"," for additional samples and Rosyln analyzer to help with coding within your IDE.",[318,468,320],{},{"title":11,"searchDepth":12,"depth":12,"links":470},[],"2019-12-10T17:37:51.332Z","Yes! There really are 6 ways to compare strings within .NET (I suspect other languages as well).  Hopefully this will shed some light on the options.","\u002Farticles\u002Fimages\u002FgzWyiS6VDt.png",{},"\u002Farticles\u002Fstring-comparisons",{"title":334,"description":472},"articles\u002Fstring-comparisons",[479,18],"netcore","Ib87YXw71UKKaBTOHX5m6SHBrTG_PcVtHJ-LK6igPl0",{"id":482,"title":483,"author":28,"body":484,"createdAt":508,"description":509,"extension":15,"img":510,"meta":511,"navigation":19,"path":512,"seo":513,"stem":514,"tags":515,"updatedAt":508,"__hash__":516},"articles\u002Farticles\u002Funable-to-save-database-diagrams.md","Unable to save database diagrams",{"type":8,"value":485,"toc":506},[486,489,499],[32,487,488],{},"It sounds like your sysdiagrams table somehow lost the IDENTITY property on the diagram_id column.  Using the following SQL I was able to drop the sysdiagrams table and rebuild.  Note you will lose any existing diagrams (if you had any during this process)",[39,490,491,494],{},[44,492,493],{},"DROP TABLE dbo.sysdiagrams;\nGO\nCREATE TABLE [dbo].[sysdiagrams]\n(\n    [name] [sysname] NOT NULL,\n    [principal_id] [int] NOT NULL,\n    [diagram_id] [int] IDENTITY(1,1) PRIMARY KEY,\n    [version] [int] NULL,\n    [definition] [varbinary](max) NULL,\n    CONSTRAINT [UK_principal_name] UNIQUE ([principal_id],[name])\n);\n",[32,495,496],{},[44,497,498],{},"GO\nEXEC sys.sp_addextendedproperty\n@name=N'microsoft_database_tools_support',\n@value=1 ,\n@level0type=N'SCHEMA',\n@level0name=N'dbo',\n@level1type=N'TABLE',\n@level1name=N'sysdiagrams';\nGO",[32,500,501,502],{},"Note: ",[459,503,505],{"href":504},"https:\u002F\u002Fdocs.microsoft.com\u002Fen-us\u002Fsql\u002Fssms\u002Fsql-server-management-studio-changelog-ssms?view=sql-server-2017","SSMS v18 release notes",{"title":11,"searchDepth":12,"depth":12,"links":507},[],"2018-10-03 13:11:00","Once you resolve the ability to save database diagrams, please note that in the next release of SSMS v18 Microsoft is deprecating database diagram feature.  I don't quite understand this thinking however you can find more information in the release notes.","\u002Farticles\u002Fimages\u002FrGWupsowyf.png",{},"\u002Farticles\u002Funable-to-save-database-diagrams",{"title":483,"description":509},"articles\u002Funable-to-save-database-diagrams",[18],"Maa_m8xqrM2uhGGESY8pwhI8NqvDOGnAZPMbdyjrRtY",{"id":518,"title":519,"author":28,"body":520,"createdAt":546,"description":547,"extension":15,"img":548,"meta":549,"navigation":19,"path":550,"seo":551,"stem":552,"tags":553,"updatedAt":546,"__hash__":555},"articles\u002Farticles\u002Fsql-server-2008-installation-ndash-not-so-quick.md","SQL Server 2008 Installation, not so quick",{"type":8,"value":521,"toc":544},[522,530,533,541],[32,523,524,525,529],{},"Before you rush to download SQL 2008 you may want to read the following KB article which warns that Visual Studio 2008 SP1 ‘may be required’ for SQL Server 2008 installations KB956139 (found ",[459,526,528],{"href":527,"target":462},"http:\u002F\u002Fsupport.microsoft.com\u002Fkb\u002F956139","here",")",[32,531,532],{},"”Because certain SQL Server 2008 features install components that are also part of the release version of Visual Studio 2008 SP1, SQL Server 2008 requires Visual Studio 2008 with SP1. If Visual Studio 2008 without a service pack is installed instead, it may not work correctly after you install SQL Server 2008.“",[32,534,535,536,540],{},"Currently, ",[459,537,539],{"href":538,"target":462},"http:\u002F\u002Fmsdn.microsoft.com\u002Fen-us\u002Fvstudio\u002Fcc533448.aspx","Visual Studio 2008 Service Pack 1"," (SP1) is in beta at the current time.",[32,542,543],{},"I do not know the expected delivery of SP1 but should be soon (hang tight).",{"title":11,"searchDepth":12,"depth":12,"links":545},[],"2017-05-29T20:59:15.8643544Z","Before you rush to download SQL 2008 you may want to read the following KB article contained within this blog.","\u002Farticles\u002Fimages\u002Fl2oWZnThPG.png",{},"\u002Farticles\u002Fsql-server-2008-installation-ndash-not-so-quick",{"title":519,"description":547},"articles\u002Fsql-server-2008-installation-ndash-not-so-quick",[554,18],"sqlserver","uUgrYz6hVSOWfcqo-ApYDrtjGpXT3uL0jd9PBGOi3E8",{"id":557,"title":558,"author":28,"body":559,"createdAt":614,"description":615,"extension":15,"img":608,"meta":616,"navigation":19,"path":617,"seo":618,"stem":619,"tags":620,"updatedAt":614,"__hash__":621},"articles\u002Farticles\u002Ffind-all-tables-containing-column-with-specified-name.md","Find all tables containing column with specified name",{"type":8,"value":560,"toc":612},[561,564,599,610],[32,562,563],{},"In order to find a column name anywhere it is used in a table or view I was able to use the\nfollowing SQL…",[39,565,567],{"className":41,"code":566,"language":18,"meta":11,"style":11},"SELECT      COLUMN_NAME AS 'ColumnName'  \n            ,TABLE_NAME AS  'TableName'  \nFROM        INFORMATION_SCHEMA.COLUMNS  \nWHERE       COLUMN_NAME LIKE '%columnnameyouarelookingfor%'  \nORDER BY    TableName  \n            ,ColumnName;  \n",[44,568,569,574,579,584,589,594],{"__ignoreMap":11},[47,570,571],{"class":49,"line":50},[47,572,573],{},"SELECT      COLUMN_NAME AS 'ColumnName'  \n",[47,575,576],{"class":49,"line":12},[47,577,578],{},"            ,TABLE_NAME AS  'TableName'  \n",[47,580,581],{"class":49,"line":61},[47,582,583],{},"FROM        INFORMATION_SCHEMA.COLUMNS  \n",[47,585,586],{"class":49,"line":67},[47,587,588],{},"WHERE       COLUMN_NAME LIKE '%columnnameyouarelookingfor%'  \n",[47,590,591],{"class":49,"line":73},[47,592,593],{},"ORDER BY    TableName  \n",[47,595,596],{"class":49,"line":79},[47,597,598],{},"            ,ColumnName;\n",[459,600,603],{"style":601,"href":602},"display:none","\u002Farticles\u002Fimages\u002Fopen-live-writer-f5f906eb5cd0_bcd7-sql_2.png",[604,605],"img",{"title":18,"style":606,"border":607,"alt":18,"src":608,"width":609,"height":609},"border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; padding-right: 0px; border-top-width: 0px;display='none';",0,"\u002Farticles\u002Fimages\u002Fopen-live-writer-f5f906eb5cd0_bcd7-sql_thumb.png",240,[318,611,320],{},{"title":11,"searchDepth":12,"depth":12,"links":613},[],"2017-01-14T06:29:42.0000000-05:00","Find a column name anywhere.",{},"\u002Farticles\u002Ffind-all-tables-containing-column-with-specified-name",{"title":558,"description":615},"articles\u002Ffind-all-tables-containing-column-with-specified-name",[18],"D4x_b6dLDTOhJk_oDi11rJkwS-ARV4n6opJh-YLJXvE",{"id":623,"title":624,"author":28,"body":625,"createdAt":758,"description":759,"extension":15,"img":644,"meta":760,"navigation":19,"path":761,"seo":762,"stem":763,"tags":764,"updatedAt":758,"__hash__":765},"articles\u002Farticles\u002Fgenerate-insert-update-delete-trigger-for-table-audit.md","Generate Insert, Update, Delete Trigger for Table Audit",{"type":8,"value":626,"toc":756},[627,633,648,651,660,754],[32,628,629,630,632],{}," ",[406,631],{},"\nThe following script will evaluate the table specified @TableName (you provide below), create a new table with {name}_Audit and also create insert, update and delete triggers.  In my example my table was Dave_Test in scheme engis.",[32,634,635,636,638],{},"Here is a screen shot of the end result audit table with the same columns PLUS 2 additional columns to document the action executed against the source table.",[406,637],{},[459,639,641],{"href":640},"\u002Farticles\u002Fimages\u002Fwindows-live-writer-generate-insert-update-delete-trigger-fo_9e1f-image_2.png",[604,642],{"style":643,"src":644,"border":607,"alt":645,"title":645,"width":646,"height":647},"background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;","\u002Farticles\u002Fimages\u002Fwindows-live-writer-generate-insert-update-delete-trigger-fo_9e1f-image_thumb.png","image",521,109,[32,649,650],{},"Here you can see the created _Audit table and newly created _Trigger_Delete, _Trigger_Insert and _Trigger_Update",[32,652,653],{},[459,654,656],{"href":655},"\u002Farticles\u002Fimages\u002Fwindows-live-writer-generate-insert-update-delete-trigger-fo_9e1f-image_6.png",[604,657],{"style":643,"src":658,"border":607,"alt":645,"title":645,"width":609,"height":659},"\u002Farticles\u002Fimages\u002Fwindows-live-writer-generate-insert-update-delete-trigger-fo_9e1f-image_thumb_2.png",183,[39,661,663],{"className":41,"code":662,"language":18,"meta":11,"style":11},"SET ANSI_NULLS ON; GO SET QUOTED_IDENTIFIER ON; GO  \nDECLARE @TableName VARCHAR(200); \nSET @TableName = 'dave_test'; \n-- SET NOCOUNT ON added to prevent extra result sets from \n-- interfering with SELECT statements. \nSET NOCOUNT ON; \n--DECLARE @TABLENAME varchar(100) \nDECLARE @SCHEMA VARCHAR(100); \n--SET @TABLENAME = N'Company' SET @SCHEMA = N'engis';\nDECLARE @Done BIT;\nSET @Done = 0 DECLARE @CRLF CHAR(2);\nSET @CRLF = CHAR(10); DECLARE @SQL VARCHAR(2000); \nSET @SQL = 'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Audit]'') AND type in (N''U'')) DROP TABLE [' + @SCHEMA + '].[' + @TableName + '_Audit] CREATE TABLE [' + @SCHEMA + '].[' + @TableName + '_Audit] (' + @CRLF; DECLARE @COLUMNID INT; SET @COLUMNID = 0; DECLARE @COLUMNNAME VARCHAR(1000); DECLARE @COLUMNTYPE VARCHAR(100); DECLARE @COLUMNSIZE INT; WHILE @Done = 0 BEGIN SELECT TOP 1 @COLUMNID = clmns.column_id , @COLUMNNAME = clmns.name , @COLUMNTYPE = usrt.name , @COLUMNSIZE = CAST(CASE WHEN baset.name IN ( N'nchar', N'nvarchar' ) AND clmns.max_length &lt;&gt; -1 THEN clmns.max_length \u002F 2 ELSE clmns.max_length END AS INT) FROM sys.tables AS tbl INNER JOIN sys.all_columns AS clmns ON clmns.object_id = tbl.object_id LEFT OUTER JOIN sys.types AS usrt ON usrt.user_type_id = clmns.user_type_id LEFT OUTER JOIN sys.types AS baset ON baset.user_type_id = clmns.system_type_id AND baset.user_type_id = baset.system_type_id WHERE ( tbl.name = @TableName AND SCHEMA_NAME(tbl.schema_id) = @SCHEMA ) AND clmns.column_id &gt; @COLUMNID ORDER BY clmns.column_id ASC; IF @@rowcount = 0 BEGIN SET @Done = 1; END; ELSE BEGIN SET @SQL = @SQL + ' [' + @COLUMNNAME + '] [' + @COLUMNTYPE + '] '; IF ( @COLUMNTYPE = 'nchar' OR @COLUMNTYPE = 'nvarchar' OR @COLUMNTYPE = 'varchar' ) SET @SQL = @SQL + '(' + LTRIM(STR(@COLUMNSIZE)) + ') '; \nSET @SQL = @SQL + 'NULL, ' + @CRLF; END; END; SET @SQL = @SQL + ' [' + @TableName + 'UpdateDate] datetime, [' + @TableName + 'UpdateAction] nvarchar(10) ) '; --print @SQL EXEC (@SQL);\nSET @SQL = ' IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Trigger_Update]'')) DROP TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Update] IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Trigger_Delete]'')) DROP TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Delete] IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Trigger_Insert]'')) DROP TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Insert] '; --print @SQL EXEC(@SQL); SET @SQL = 'CREATE TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Update] ON [' + @SCHEMA + '].[' + @TableName + '] AFTER UPDATE AS BEGIN SET NOCOUNT ON; INSERT INTO ' + @TableName + '_Audit SELECT *,getdate(),''Update'' FROM inserted END '; EXEC (@SQL); --print @SQL SET @SQL = 'CREATE TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Delete] ON [' + @SCHEMA + '].[' + @TableName + '] AFTER DELETE AS BEGIN SET NOCOUNT ON; INSERT INTO ' + @TableName + '_Audit SELECT *,getdate(),''Delete'' FROM deleted END ';\n\nEXEC (@SQL); --print @SQL SET @SQL = 'CREATE TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Insert] ON [' + @SCHEMA + '].[' + @TableName + '] AFTER INSERT AS BEGIN SET NOCOUNT ON; INSERT INTO ' + @TableName + '_Audit SELECT *,getdate(),''Insert'' FROM inserted END '; EXEC (@SQL);\n--print @SQL\n",[44,664,665,670,675,680,685,690,695,700,705,710,715,720,725,730,735,740,744,749],{"__ignoreMap":11},[47,666,667],{"class":49,"line":50},[47,668,669],{},"SET ANSI_NULLS ON; GO SET QUOTED_IDENTIFIER ON; GO  \n",[47,671,672],{"class":49,"line":12},[47,673,674],{},"DECLARE @TableName VARCHAR(200); \n",[47,676,677],{"class":49,"line":61},[47,678,679],{},"SET @TableName = 'dave_test'; \n",[47,681,682],{"class":49,"line":67},[47,683,684],{},"-- SET NOCOUNT ON added to prevent extra result sets from \n",[47,686,687],{"class":49,"line":73},[47,688,689],{},"-- interfering with SELECT statements. \n",[47,691,692],{"class":49,"line":79},[47,693,694],{},"SET NOCOUNT ON; \n",[47,696,697],{"class":49,"line":84},[47,698,699],{},"--DECLARE @TABLENAME varchar(100) \n",[47,701,702],{"class":49,"line":90},[47,703,704],{},"DECLARE @SCHEMA VARCHAR(100); \n",[47,706,707],{"class":49,"line":95},[47,708,709],{},"--SET @TABLENAME = N'Company' SET @SCHEMA = N'engis';\n",[47,711,712],{"class":49,"line":101},[47,713,714],{},"DECLARE @Done BIT;\n",[47,716,717],{"class":49,"line":107},[47,718,719],{},"SET @Done = 0 DECLARE @CRLF CHAR(2);\n",[47,721,722],{"class":49,"line":112},[47,723,724],{},"SET @CRLF = CHAR(10); DECLARE @SQL VARCHAR(2000); \n",[47,726,727],{"class":49,"line":118},[47,728,729],{},"SET @SQL = 'IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Audit]'') AND type in (N''U'')) DROP TABLE [' + @SCHEMA + '].[' + @TableName + '_Audit] CREATE TABLE [' + @SCHEMA + '].[' + @TableName + '_Audit] (' + @CRLF; DECLARE @COLUMNID INT; SET @COLUMNID = 0; DECLARE @COLUMNNAME VARCHAR(1000); DECLARE @COLUMNTYPE VARCHAR(100); DECLARE @COLUMNSIZE INT; WHILE @Done = 0 BEGIN SELECT TOP 1 @COLUMNID = clmns.column_id , @COLUMNNAME = clmns.name , @COLUMNTYPE = usrt.name , @COLUMNSIZE = CAST(CASE WHEN baset.name IN ( N'nchar', N'nvarchar' ) AND clmns.max_length &lt;&gt; -1 THEN clmns.max_length \u002F 2 ELSE clmns.max_length END AS INT) FROM sys.tables AS tbl INNER JOIN sys.all_columns AS clmns ON clmns.object_id = tbl.object_id LEFT OUTER JOIN sys.types AS usrt ON usrt.user_type_id = clmns.user_type_id LEFT OUTER JOIN sys.types AS baset ON baset.user_type_id = clmns.system_type_id AND baset.user_type_id = baset.system_type_id WHERE ( tbl.name = @TableName AND SCHEMA_NAME(tbl.schema_id) = @SCHEMA ) AND clmns.column_id &gt; @COLUMNID ORDER BY clmns.column_id ASC; IF @@rowcount = 0 BEGIN SET @Done = 1; END; ELSE BEGIN SET @SQL = @SQL + ' [' + @COLUMNNAME + '] [' + @COLUMNTYPE + '] '; IF ( @COLUMNTYPE = 'nchar' OR @COLUMNTYPE = 'nvarchar' OR @COLUMNTYPE = 'varchar' ) SET @SQL = @SQL + '(' + LTRIM(STR(@COLUMNSIZE)) + ') '; \n",[47,731,732],{"class":49,"line":123},[47,733,734],{},"SET @SQL = @SQL + 'NULL, ' + @CRLF; END; END; SET @SQL = @SQL + ' [' + @TableName + 'UpdateDate] datetime, [' + @TableName + 'UpdateAction] nvarchar(10) ) '; --print @SQL EXEC (@SQL);\n",[47,736,737],{"class":49,"line":129},[47,738,739],{},"SET @SQL = ' IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Trigger_Update]'')) DROP TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Update] IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Trigger_Delete]'')) DROP TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Delete] IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N''[' + @SCHEMA + '].[' + @TableName + '_Trigger_Insert]'')) DROP TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Insert] '; --print @SQL EXEC(@SQL); SET @SQL = 'CREATE TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Update] ON [' + @SCHEMA + '].[' + @TableName + '] AFTER UPDATE AS BEGIN SET NOCOUNT ON; INSERT INTO ' + @TableName + '_Audit SELECT *,getdate(),''Update'' FROM inserted END '; EXEC (@SQL); --print @SQL SET @SQL = 'CREATE TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Delete] ON [' + @SCHEMA + '].[' + @TableName + '] AFTER DELETE AS BEGIN SET NOCOUNT ON; INSERT INTO ' + @TableName + '_Audit SELECT *,getdate(),''Delete'' FROM deleted END ';\n",[47,741,742],{"class":49,"line":135},[47,743,64],{"emptyLinePlaceholder":19},[47,745,746],{"class":49,"line":141},[47,747,748],{},"EXEC (@SQL); --print @SQL SET @SQL = 'CREATE TRIGGER [' + @SCHEMA + '].[' + @TableName + '_Trigger_Insert] ON [' + @SCHEMA + '].[' + @TableName + '] AFTER INSERT AS BEGIN SET NOCOUNT ON; INSERT INTO ' + @TableName + '_Audit SELECT *,getdate(),''Insert'' FROM inserted END '; EXEC (@SQL);\n",[47,750,751],{"class":49,"line":147},[47,752,753],{},"--print @SQL\n",[318,755,320],{},{"title":11,"searchDepth":12,"depth":12,"links":757},[],"2016-02-13T07:03:23.3300000-05:00"," \nThe following script will evaluate the table specified @TableName (you provide below), create a new table with {name}_Audit and also create insert, update and delete triggers.  In my example my table was Dave_Test in scheme engis.",{},"\u002Farticles\u002Fgenerate-insert-update-delete-trigger-for-table-audit",{"title":624,"description":759},"articles\u002Fgenerate-insert-update-delete-trigger-for-table-audit",[18],"Dt6NMP0mx0GNm-szVZbRfyE2hv-az2OeYHLGEoaDFqU",{"id":767,"title":768,"author":28,"body":769,"createdAt":1052,"description":1053,"extension":15,"img":1054,"meta":1055,"navigation":19,"path":1056,"seo":1057,"stem":1058,"tags":1059,"updatedAt":1052,"__hash__":1060},"articles\u002Farticles\u002Fsql-server-triggers-to-mirror-a-table.md","SQL Server Triggers To Mirror a Table",{"type":8,"value":770,"toc":1050},[771,777,837,840,1037,1040,1048],[32,772,773,774,776],{},"I had a need to mirror any changes to one Sql Server table to another Sql Server table of a different name. Both tables had similar columns and types. In this sample my table is called Dave_Test and my mirror table was Dave_Test_Audit.",[406,775],{},"\nI will provide the create table structures in the event you want to replicate my code.",[39,778,780],{"className":41,"code":779,"language":18,"meta":11,"style":11},"CREATE TABLE [Dave_Test]( \n    [Emp_ID] [int] IDENTITY(1,1) NOT NULL, \n    [Emp_name] [varchar](100) NULL, \n    [Emp_Sal] [decimal](10, 2) NULL ) ON [PRIMARY]  \n   \nCREATE TABLE [Dave_Test_Audit](\n    [Emp_ID] [int] NULL,\n    [Emp_name] [varchar](100) NULL,\n    [Emp_Sal] [decimal](18, 0) NULL\n) ON [PRIMARY]  \nGO  \n",[44,781,782,787,792,797,802,807,812,817,822,827,832],{"__ignoreMap":11},[47,783,784],{"class":49,"line":50},[47,785,786],{},"CREATE TABLE [Dave_Test]( \n",[47,788,789],{"class":49,"line":12},[47,790,791],{},"    [Emp_ID] [int] IDENTITY(1,1) NOT NULL, \n",[47,793,794],{"class":49,"line":61},[47,795,796],{},"    [Emp_name] [varchar](100) NULL, \n",[47,798,799],{"class":49,"line":67},[47,800,801],{},"    [Emp_Sal] [decimal](10, 2) NULL ) ON [PRIMARY]  \n",[47,803,804],{"class":49,"line":73},[47,805,806],{},"   \n",[47,808,809],{"class":49,"line":79},[47,810,811],{},"CREATE TABLE [Dave_Test_Audit](\n",[47,813,814],{"class":49,"line":84},[47,815,816],{},"    [Emp_ID] [int] NULL,\n",[47,818,819],{"class":49,"line":90},[47,820,821],{},"    [Emp_name] [varchar](100) NULL,\n",[47,823,824],{"class":49,"line":95},[47,825,826],{},"    [Emp_Sal] [decimal](18, 0) NULL\n",[47,828,829],{"class":49,"line":101},[47,830,831],{},") ON [PRIMARY]  \n",[47,833,834],{"class":49,"line":107},[47,835,836],{},"GO\n",[32,838,839],{},"Here are my insert, update and delete triggers.",[39,841,843],{"className":41,"code":842,"language":18,"meta":11,"style":11},"Create TRIGGER [Dave_Test_Trigger_Delete]   \n   ON  [Dave_Test]   \n   AFTER DELETE  \nAS  \nBEGIN  \n     SET NOCOUNT ON;  \n     DELETE FROM Dave_Test_Audit   \n            WHERE emp_id IN (SELECT emp_id FROM deleted)  \nEND  \n  \nCreate TRIGGER [Dave_Test_Trigger_Insert]   \n   ON  [Dave_Test]   \n   AFTER INSERT  \nAS  \nBEGIN     SET NOCOUNT ON;  \n        INSERT INTO Dave_Test_Audit   \n         SELECT * FROM inserted END  \nEND  \n  \nCREATE TRIGGER [[Dave_Test_Trigger_Update]   \n   ON  [Dave_Test]   \n   AFTER UPDATE  \nAS  \nBEGIN  \n        SET NOCOUNT ON;  \n        IF EXISTS(SELECT * FROM Dave_Test_Audit a JOIN inserted AS i ON a.emp_id=i.emp_id)          \n        BEGIN  \n              UPDATE  Dave_Test_Audit   \n                SET emp_id = i.emp_id,  \n                emp_name = i.emp_name,  \n                emp_sal =  i.emp_sal  \n                FROM inserted i WHERE Dave_Test_Audit.emp_id=i.emp_id           \n              \n        END  \n        ELSE  \n             BEGIN  \n             INSERT INTO Dave_Test_Audit   \n                SELECT * FROM inserted   \n        END        \nEND\n",[44,844,845,850,855,860,865,870,875,880,885,890,895,900,904,909,913,918,923,928,932,936,941,945,950,954,958,963,968,973,978,983,988,993,998,1003,1008,1013,1018,1023,1028,1033],{"__ignoreMap":11},[47,846,847],{"class":49,"line":50},[47,848,849],{},"Create TRIGGER [Dave_Test_Trigger_Delete]   \n",[47,851,852],{"class":49,"line":12},[47,853,854],{},"   ON  [Dave_Test]   \n",[47,856,857],{"class":49,"line":61},[47,858,859],{},"   AFTER DELETE  \n",[47,861,862],{"class":49,"line":67},[47,863,864],{},"AS  \n",[47,866,867],{"class":49,"line":73},[47,868,869],{},"BEGIN  \n",[47,871,872],{"class":49,"line":79},[47,873,874],{},"     SET NOCOUNT ON;  \n",[47,876,877],{"class":49,"line":84},[47,878,879],{},"     DELETE FROM Dave_Test_Audit   \n",[47,881,882],{"class":49,"line":90},[47,883,884],{},"            WHERE emp_id IN (SELECT emp_id FROM deleted)  \n",[47,886,887],{"class":49,"line":95},[47,888,889],{},"END  \n",[47,891,892],{"class":49,"line":101},[47,893,894],{},"  \n",[47,896,897],{"class":49,"line":107},[47,898,899],{},"Create TRIGGER [Dave_Test_Trigger_Insert]   \n",[47,901,902],{"class":49,"line":112},[47,903,854],{},[47,905,906],{"class":49,"line":118},[47,907,908],{},"   AFTER INSERT  \n",[47,910,911],{"class":49,"line":123},[47,912,864],{},[47,914,915],{"class":49,"line":129},[47,916,917],{},"BEGIN     SET NOCOUNT ON;  \n",[47,919,920],{"class":49,"line":135},[47,921,922],{},"        INSERT INTO Dave_Test_Audit   \n",[47,924,925],{"class":49,"line":141},[47,926,927],{},"         SELECT * FROM inserted END  \n",[47,929,930],{"class":49,"line":147},[47,931,889],{},[47,933,934],{"class":49,"line":152},[47,935,894],{},[47,937,938],{"class":49,"line":158},[47,939,940],{},"CREATE TRIGGER [[Dave_Test_Trigger_Update]   \n",[47,942,943],{"class":49,"line":163},[47,944,854],{},[47,946,947],{"class":49,"line":169},[47,948,949],{},"   AFTER UPDATE  \n",[47,951,952],{"class":49,"line":175},[47,953,864],{},[47,955,956],{"class":49,"line":180},[47,957,869],{},[47,959,960],{"class":49,"line":186},[47,961,962],{},"        SET NOCOUNT ON;  \n",[47,964,965],{"class":49,"line":192},[47,966,967],{},"        IF EXISTS(SELECT * FROM Dave_Test_Audit a JOIN inserted AS i ON a.emp_id=i.emp_id)          \n",[47,969,970],{"class":49,"line":198},[47,971,972],{},"        BEGIN  \n",[47,974,975],{"class":49,"line":204},[47,976,977],{},"              UPDATE  Dave_Test_Audit   \n",[47,979,980],{"class":49,"line":210},[47,981,982],{},"                SET emp_id = i.emp_id,  \n",[47,984,985],{"class":49,"line":215},[47,986,987],{},"                emp_name = i.emp_name,  \n",[47,989,990],{"class":49,"line":221},[47,991,992],{},"                emp_sal =  i.emp_sal  \n",[47,994,995],{"class":49,"line":227},[47,996,997],{},"                FROM inserted i WHERE Dave_Test_Audit.emp_id=i.emp_id           \n",[47,999,1000],{"class":49,"line":233},[47,1001,1002],{},"              \n",[47,1004,1005],{"class":49,"line":238},[47,1006,1007],{},"        END  \n",[47,1009,1010],{"class":49,"line":244},[47,1011,1012],{},"        ELSE  \n",[47,1014,1015],{"class":49,"line":250},[47,1016,1017],{},"             BEGIN  \n",[47,1019,1020],{"class":49,"line":255},[47,1021,1022],{},"             INSERT INTO Dave_Test_Audit   \n",[47,1024,1025],{"class":49,"line":260},[47,1026,1027],{},"                SELECT * FROM inserted   \n",[47,1029,1030],{"class":49,"line":266},[47,1031,1032],{},"        END        \n",[47,1034,1035],{"class":49,"line":271},[47,1036,230],{},[32,1038,1039],{},"So why does my Update trigger deal with inserts?  The master table (Dave_Test) already had rows existing before the triggers were created.  With the update trigger managing inserting and updating records if the record is updated and does yet belong in the master audit table then the recently updated row will be inserted into the audit table for us.",[32,1041,1042,1043,1047],{},"For those old enough – this is ",[459,1044,1046],{"href":1045,"target":462},"http:\u002F\u002Fwww.happytrails.org\u002Ftrigger.html","Trigger"," (the smartest horse in the movies)",[318,1049,320],{},{"title":11,"searchDepth":12,"depth":12,"links":1051},[],"2016-02-13T07:02:53.5100000-05:00","I had a need to mirror any changes to one Sql Server table to another Sql Server table of a different name.  Read on.","\u002Farticles\u002Fimages\u002FXp0SJTsB7n.png",{},"\u002Farticles\u002Fsql-server-triggers-to-mirror-a-table",{"title":768,"description":1053},"articles\u002Fsql-server-triggers-to-mirror-a-table",[18,554],"oG-qazOpjUVf0yb-VL3IN5WX57TFCezPsA53rRrA00k",{"id":1062,"title":1063,"author":28,"body":1064,"createdAt":1109,"description":1110,"extension":15,"img":1111,"meta":1112,"navigation":19,"path":1113,"seo":1114,"stem":1115,"tags":1116,"updatedAt":1109,"__hash__":1117},"articles\u002Farticles\u002Fsql-server-and-find-all-columns-in-a-database.md","SQL Server and Find all Columns in a Database",{"type":8,"value":1065,"toc":1107},[1066,1069,1099,1105],[32,1067,1068],{},"Where the column name matches exactly…",[39,1070,1072],{"className":41,"code":1071,"language":18,"meta":11,"style":11},"SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns\nWHERE name = '{text}' )\nWhere the column name is like…\nSELECT name FROM sysobjects WHERE id IN \n    ( SELECT id FROM syscolumns WHERE name like '%{text}%' )  \n",[44,1073,1074,1079,1084,1089,1094],{"__ignoreMap":11},[47,1075,1076],{"class":49,"line":50},[47,1077,1078],{},"SELECT name FROM sysobjects WHERE id IN ( SELECT id FROM syscolumns\n",[47,1080,1081],{"class":49,"line":12},[47,1082,1083],{},"WHERE name = '{text}' )\n",[47,1085,1086],{"class":49,"line":61},[47,1087,1088],{},"Where the column name is like…\n",[47,1090,1091],{"class":49,"line":67},[47,1092,1093],{},"SELECT name FROM sysobjects WHERE id IN \n",[47,1095,1096],{"class":49,"line":73},[47,1097,1098],{},"    ( SELECT id FROM syscolumns WHERE name like '%{text}%' )\n",[32,1100,1101,1102,1104],{},"The following will show all occurrences of the column in any object",[406,1103],{},"\nSELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME LIKE '%doctorid%'",[318,1106,320],{},{"title":11,"searchDepth":12,"depth":12,"links":1108},[],"2015-04-20T08:07:16.9200000-04:00","Find all occurrences of the column in any object.","\u002Farticles\u002Fimages\u002F01dQC8VR9Q.png",{},"\u002Farticles\u002Fsql-server-and-find-all-columns-in-a-database",{"title":1063,"description":1110},"articles\u002Fsql-server-and-find-all-columns-in-a-database",[18],"7j7MoIgvvTBgGhiyTLQS8O1mSrOntcrI4ggv0hVclEs",{"id":1119,"title":1120,"author":28,"body":1121,"createdAt":2119,"description":2120,"extension":15,"img":2116,"meta":2121,"navigation":19,"path":2122,"seo":2123,"stem":2124,"tags":2125,"updatedAt":2119,"__hash__":2126},"articles\u002Farticles\u002Fexport-sql-data-to-microsoft-excel-using-visual-studio-2008-c.md","Export SQL Data to Microsoft Excel (using Visual Studio 2008, c#)",{"type":8,"value":1122,"toc":2117},[1123,1132,1147,1264,1337,1340,1370,1473,1601,1840,1991,2056,2113],[32,1124,1125,1126,1128,1129,1131],{},"Over the years there have been a number of methods to move SQL data into Microsoft Excel.  Using Automation you can call methods and properties that are specific to excel which gives you the greatest flexibility for specifying the location of your data in the workbook.  The following are two recent approaches to export Sql Server table row data to an excel workbook. ",[406,1127],{},"\n1.) Using Automation you can use transfer data cell by cell",[406,1130],{},"\n2.) Transfer data in an array to a range of cells",[32,1133,1134,1135,1137,1138,1140,1141,1143,1144,1146],{},"I created a WPF application with a button \"Export\" for this code.  The click event creates performs the the following actions",[406,1136],{},"\na.) Gets a reference to the automation object\nb.) Adds a default workbook\nc.) Gets the list of tables within the database\nd.) Gets data\ne.) Exports the data to excel worksheets (there are 2 methods defined (1) range method (2) cell by cell\nf.) Saves the excel sheet\ng.) Clean up",[406,1139],{},"\n ",[406,1142],{},"\nThis blog posts shows two ways to extract data from SQL Server and place into an Excel document. The Range method was much quicker than the cell by cell approach.  The code below gets a list of all tables within a particular SQL Database and exports the data to Excel. ",[406,1145],{},"\nI didn’t use was Excel 2007 document format at this.  If you have a good post or web link using this technique I would happily reference.  Let me know if you would like the Visual Studio project.",[1148,1149,894,1152,894],"div",{"id":1150,"style":1151},"codeSnippetWrapper","text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;",[1148,1153,894,1156,894,894,1172,894,894,1181,894,894,1183,894,894,1186,894,894,1193,894,894,1195,894,894,1202,894,894,1205,894,894,1207,894,1163,894,1210,894,894,1215,894,894,1220,894,894,1227,894,894,1229,894,894,1234,894,1163,894,1239,894,894,1249,894,894,1252,894,894,1255,894,894,1261,894],{"id":1154,"style":1155},"codeSnippet","text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[39,1157,1159,1163,1164,1167,1168,1171],{"style":1158},"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[47,1160,1162],{"style":1161},"color: #0000ff;","private"," ",[47,1165,1166],{"style":1161},"void"," btnExport_Click(",[47,1169,1170],{"style":1161},"object"," sender, RoutedEventArgs e) {",[39,1173,1175,1176,1180],{"style":1174},"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;","    lblMessage.Content=",[47,1177,1179],{"style":1178},"color: #006080;","\"Export Started..\"",";",[39,1182,629],{"style":1158},[39,1184,1185],{"style":1174},"    WpfApplication.DoEvents();",[39,1187,1188,1189,1192],{"style":1158},"    ApplicationClass app = CreateExcelDocument(",[47,1190,1191],{"style":1161},"false",");",[39,1194,629],{"style":1174},[39,1196,1197,1198],{"style":1158},"    ",[47,1199,1201],{"style":1200},"color: #008000;","\u002F\u002Fadd workbook to excel document",[39,1203,1204],{"style":1174},"    Workbook workbook = app.Workbooks.Add(Type.Missing);",[39,1206,629],{"style":1158},[39,1208,1209],{"style":1174},"    DataSet ds = GetData(GetTableList());            ",[39,1211,1197,1212],{"style":1174},[47,1213,1214],{"style":1200},"\u002F\u002Fshown below are 2 method of moving data from sql to excel, only use one of them",[39,1216,1197,1217],{"style":1158},[47,1218,1219],{"style":1200},"\u002F\u002F(1)call method to export to worksheet using range functionality (much faster than cell by cell method",[39,1221,1222,1223,1226],{"style":1174},"    Export(ds, ",[47,1224,1225],{"style":1161},"ref"," workbook);",[39,1228,629],{"style":1158},[39,1230,1197,1231],{"style":1174},[47,1232,1233],{"style":1200},"\u002F\u002F(2)cell by cell method",[39,1235,1197,1236],{"style":1158},[47,1237,1238],{"style":1200},"\u002F\u002FInsertIntoExcel(app, ds);",[39,1240,1241,1242,1245,1246,1180],{"style":1158},"     ",[47,1243,1244],{"style":1161},"string"," fileName = ",[47,1247,1248],{"style":1178},"@\"C:\\Safety3.xlsx\"",[39,1250,1251],{"style":1174},"     SaveDoc(workbook, fileName);",[39,1253,1254],{"style":1158},"     CloseExcelDocument(app);",[39,1256,1257,1258,1180],{"style":1174},"     lblMessage.Content = ",[47,1259,1260],{"style":1178},"\"Finished..\"",[39,1262,1263],{"style":1158},"}",[1148,1265,894,1266,894],{"id":1150,"style":1151},[1148,1267,894,1268,894,894,1273,894,894,1278,894,894,1283,894,894,1292,894,894,1298,894,894,1304,894,894,1311,894,894,1314,894,894,1316,894,894,1323,894,894,1326,894,894,1329,894,894,1335,894],{"id":1154,"style":1155},[39,1269,1270],{"style":1158},[47,1271,1272],{"style":1200},"\u002F\u002F\u002F \u003Csummary>",[39,1274,1275],{"style":1174},[47,1276,1277],{"style":1200},"\u002F\u002F\u002F creates excel document",[39,1279,1280],{"style":1158},[47,1281,1282],{"style":1200},"\u002F\u002F\u002F \u003C\u002Fsummary>",[39,1284,1285,1287,1288,1291],{"style":1174},[47,1286,1162],{"style":1161}," ApplicationClass CreateExcelDocument(",[47,1289,1290],{"style":1161},"bool"," visible) {",[39,1293,1294,1295,1180],{"style":1158},"    ApplicationClass app = ",[47,1296,1297],{"style":1161},"null",[39,1299,1197,1300,1303],{"style":1174},[47,1301,1302],{"style":1161},"try"," {",[39,1305,1306,1307,1310],{"style":1158},"        app = ",[47,1308,1309],{"style":1161},"new"," Microsoft.Office.Interop.Excel.ApplicationClass();",[39,1312,1313],{"style":1174},"        app.Visible = visible;",[39,1315,629],{"style":1158},[39,1317,1318,1319,1322],{"style":1174},"    } ",[47,1320,1321],{"style":1161},"catch"," (Exception ex) {",[39,1324,1325],{"style":1158},"        MessageBox.Show(ex.ToString());",[39,1327,1328],{"style":1174},"    }",[39,1330,1197,1331,1334],{"style":1158},[47,1332,1333],{"style":1161},"return"," app;",[39,1336,1263],{"style":1174},[32,1338,1339],{},"c.) The following method GetTableList() returns a List\u003CExportInfo> where ExportInfo is a structure that has the Name and SQL to be used for each table to be exported.",[1148,1341,894,1342,894],{"id":1150},[1148,1343,894,1345,894,894,1354,894,894,1361,894,894,1368,894],{"id":1154,"style":1344},"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; height: 63px; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[39,1346,1347,1163,1350,1353],{"style":1158},[47,1348,1349],{"style":1161},"public",[47,1351,1352],{"style":1161},"class"," ExportInfo {",[39,1355,1197,1356,1163,1358,1360],{"style":1174},[47,1357,1349],{"style":1161},[47,1359,1244],{"style":1161}," Name { get; set; }",[39,1362,1197,1363,1163,1365,1367],{"style":1158},[47,1364,1349],{"style":1161},[47,1366,1244],{"style":1161}," Sql { get; set; }",[39,1369,1263],{"style":1174},[1148,1371,894,1373,894],{"id":1150,"style":1372},"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; height: 324px; max-height: 350px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;",[1148,1374,894,1375,894,894,1379,894,894,1384,894,894,1388,894,894,1393,894,894,1398,894,894,1407,894,1163,894,1416,894,894,1419,894,894,1425,894,894,1428,894,894,1431,894,894,1434,894,894,1436,894,894,1438,894,894,1444,894,894,1454,894,894,1464,894,894,1466,894,894,1471,894],{"id":1154,"style":1155},[39,1376,1377],{"style":1158},[47,1378,1272],{"style":1200},[39,1380,1381],{"style":1174},[47,1382,1383],{"style":1200},"\u002F\u002F\u002F get list of sql tables in database",[39,1385,1386],{"style":1158},[47,1387,1282],{"style":1200},[39,1389,1390],{"style":1174},[47,1391,1392],{"style":1200},"\u002F\u002F\u002F \u003Creturns>list of ExportInfo objects\u003C\u002Freturns>",[39,1394,1395,1397],{"style":1158},[47,1396,1162],{"style":1161}," List\u003CExportInfo> GetTableList() {",[39,1399,1400,1401,1403,1404,1192],{"style":1174},"    System.Data.DataTable tables = ",[47,1402,1309],{"style":1161}," System.Data.DataTable(",[47,1405,1406],{"style":1178},"\"Tables\"",[39,1408,1197,1409,1412,1413,1415],{"style":1158},[47,1410,1411],{"style":1161},"using"," (SqlConnection connection = ",[47,1414,1309],{"style":1161}," SqlConnection(GetConnectionString())) {",[39,1417,1418],{"style":1158},"        SqlCommand command = connection.CreateCommand();",[39,1420,1421,1422,1180],{"style":1174},"        command.CommandText = ",[47,1423,1424],{"style":1178},"\"select table_name as Name from INFORMATION_SCHEMA.tables where TABLE_TYPE = 'BASE TABLE'\"",[39,1426,1427],{"style":1158},"        connection.Open();",[39,1429,1430],{"style":1174},"        tables.Load(command.ExecuteReader(CommandBehavior.CloseConnection));",[39,1432,1433],{"style":1158},"        connection.Close();",[39,1435,1328],{"style":1174},[39,1437,629],{"style":1158},[39,1439,1440,1441,1443],{"style":1174},"    List\u003CExportInfo> exps = ",[47,1442,1309],{"style":1161}," List\u003CExportInfo>();",[39,1445,1197,1446,1449,1450,1453],{"style":1158},[47,1447,1448],{"style":1161},"foreach"," (DataRow row ",[47,1451,1452],{"style":1161},"in"," tables.Rows) {",[39,1455,1456,1457,1459,1460,1463],{"style":1174},"        exps.Add(",[47,1458,1309],{"style":1161}," ExportInfo(){Name=row[0].ToString(), Sql = String.Format(",[47,1461,1462],{"style":1178},"\"select top 1 * from {0}\"",",row[0].ToString())});",[39,1465,1328],{"style":1158},[39,1467,1197,1468,1470],{"style":1174},[47,1469,1333],{"style":1161}," exps;            ",[39,1472,1263],{"style":1158},[1148,1474,894,1476,894],{"id":1150,"style":1475},"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; height: 247px; max-height: 200px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;",[1148,1477,894,1478,894,894,1482,894,894,1487,894,894,1491,894,894,1496,894,894,1501,894,894,1506,894,894,1512,894,894,1520,894,894,1528,894,894,1534,894,894,1536,894,894,1538,894,894,1543,894,894,1549,894,894,1555,894,894,1559,894,894,1562,894,894,1566,894,894,1568,894,894,1570,894,894,1572,894,894,1578,894,894,1586,894,894,1589,894,894,1592,894,894,1594,894,894,1599,894],{"id":1154,"style":1155},[39,1479,1480],{"style":1158},[47,1481,1272],{"style":1200},[39,1483,1484],{"style":1174},[47,1485,1486],{"style":1200},"\u002F\u002F\u002F get data from sql server",[39,1488,1489],{"style":1158},[47,1490,1282],{"style":1200},[39,1492,1493],{"style":1174},[47,1494,1495],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"exps\">\u003C\u002Fparam>",[39,1497,1498],{"style":1158},[47,1499,1500],{"style":1200},"\u002F\u002F\u002F \u003Creturns>dataset with return results\u003C\u002Freturns>",[39,1502,1503,1505],{"style":1174},[47,1504,1162],{"style":1161}," System.Data.DataSet GetData(List\u003CExportInfo> exps) {",[39,1507,1508,1509,1511],{"style":1158},"    DataSet ds = ",[47,1510,1309],{"style":1161}," DataSet();",[39,1513,1197,1514,1516,1517,1519],{"style":1174},[47,1515,1244],{"style":1161}," executeSql = ",[47,1518,1244],{"style":1161},".Empty;",[39,1521,1197,1522,1524,1525,1527],{"style":1158},[47,1523,1448],{"style":1161}," (ExportInfo exp ",[47,1526,1452],{"style":1161}," exps) {",[39,1529,1530,1531,1180],{"style":1174},"        executeSql += exp.Sql + ",[47,1532,1533],{"style":1178},"\";\"",[39,1535,1328],{"style":1158},[39,1537,629],{"style":1174},[39,1539,1197,1540,1542],{"style":1158},[47,1541,1244],{"style":1161}," connectionString = GetConnectionString();",[39,1544,1545,1546,1548],{"style":1174},"    SqlConnection conn = ",[47,1547,1309],{"style":1161}," SqlConnection(connectionString);",[39,1550,1551,1552,1554],{"style":1158},"    SqlDataAdapter adapter = ",[47,1553,1309],{"style":1161}," SqlDataAdapter(executeSql, conn);",[39,1556,1197,1557,1303],{"style":1174},[47,1558,1302],{"style":1161},[39,1560,1561],{"style":1158},"        adapter.Fill(ds);",[39,1563,1318,1564,1322],{"style":1174},[47,1565,1321],{"style":1161},[39,1567,629],{"style":1158},[39,1569,1328],{"style":1174},[39,1571,629],{"style":1158},[39,1573,1197,1574,1577],{"style":1174},[47,1575,1576],{"style":1161},"int"," index = 0;",[39,1579,1197,1580,1582,1583,1585],{"style":1158},[47,1581,1448],{"style":1161}," (System.Data.DataTable dt ",[47,1584,1452],{"style":1161}," ds.Tables) {",[39,1587,1588],{"style":1174},"        dt.TableName = exps[index].Name;",[39,1590,1591],{"style":1158},"        index += 1;",[39,1593,1328],{"style":1174},[39,1595,1197,1596,1598],{"style":1158},[47,1597,1333],{"style":1161}," ds;",[39,1600,1263],{"style":1174},[1148,1602,894,1604,894],{"id":1150,"style":1603},"text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; width: 97.5%; font-family: 'Courier New', courier, monospace; direction: ltr; max-height: 500px; font-size: 8pt; overflow: auto; cursor: text; border: silver 1px solid; padding: 4px;",[1148,1605,894,1606,894,894,1610,894,894,1615,894,894,1619,894,894,1624,894,894,1629,894,894,1639,894,894,1641,894,894,1646,894,894,1648,894,894,1653,894,894,1660,894,894,1662,894,894,1668,894,894,1678,894,894,1680,894,894,1685,894,894,1694,894,894,1697,894,894,1700,894,894,1702,894,894,1707,894,894,1713,894,894,1721,894,894,1724,894,894,1727,894,894,1729,894,894,1731,894,894,1736,894,894,1743,894,894,1751,894,894,1756,894,894,1758,894,894,1764,894,894,1767,894,894,1770,894,894,1772,894,894,1774,894,894,1777,894,894,1780,894,894,1782,894,894,1787,894,894,1790,894,894,1793,894,894,1795,894,894,1801,894,894,1803,894,894,1808,894,894,1820,894,894,1823,894,894,1825,894,894,1830,894,894,1836,894,894,1838,894],{"id":1154,"style":1155},[39,1607,1608],{"style":1158},[47,1609,1272],{"style":1200},[39,1611,1612],{"style":1174},[47,1613,1614],{"style":1200},"\u002F\u002F\u002F Copy data from dataset into workbook",[39,1616,1617],{"style":1158},[47,1618,1282],{"style":1200},[39,1620,1621],{"style":1174},[47,1622,1623],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"dataSet\">\u003C\u002Fparam>",[39,1625,1626],{"style":1158},[47,1627,1628],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"excelWorkbook\">\u003C\u002Fparam>        ",[39,1630,1631,1163,1633,1635,1636,1638],{"style":1174},[47,1632,1162],{"style":1161},[47,1634,1166],{"style":1161}," Export(DataSet dataSet, ",[47,1637,1225],{"style":1161}," Workbook excelWorkbook) {",[39,1640,629],{"style":1158},[39,1642,1197,1643,1645],{"style":1174},[47,1644,1576],{"style":1161}," sheetIndex = 0;",[39,1647,629],{"style":1158},[39,1649,1197,1650],{"style":1174},[47,1651,1652],{"style":1200},"\u002F\u002F build rawData 2 dimensional array with data for each datatable            ",[39,1654,1197,1655,1582,1657,1659],{"style":1158},[47,1656,1448],{"style":1161},[47,1658,1452],{"style":1161}," dataSet.Tables) {",[39,1661,629],{"style":1174},[39,1663,1664,1665],{"style":1158},"        ",[47,1666,1667],{"style":1200},"\u002F\u002F Copy the DataTable to an object array",[39,1669,1664,1670,1672,1673,1163,1675,1677],{"style":1174},[47,1671,1170],{"style":1161},"[,] data = ",[47,1674,1309],{"style":1161},[47,1676,1170],{"style":1161},"[dt.Rows.Count + 1, dt.Columns.Count];",[39,1679,629],{"style":1158},[39,1681,1664,1682],{"style":1174},[47,1683,1684],{"style":1200},"\u002F\u002F Copy the column names to the first row of the object array",[39,1686,1664,1687,1690,1691,1693],{"style":1158},[47,1688,1689],{"style":1161},"for"," (",[47,1692,1576],{"style":1161}," col = 0; col \u003C dt.Columns.Count; col++) {",[39,1695,1696],{"style":1174},"            data[0, col] = dt.Columns[col].ColumnName;",[39,1698,1699],{"style":1158},"        }",[39,1701,629],{"style":1174},[39,1703,1664,1704],{"style":1158},[47,1705,1706],{"style":1200},"\u002F\u002F Copy the values to the object array",[39,1708,1664,1709,1690,1711,1693],{"style":1174},[47,1710,1689],{"style":1161},[47,1712,1576],{"style":1161},[39,1714,1715,1716,1690,1718,1720],{"style":1158},"            ",[47,1717,1689],{"style":1161},[47,1719,1576],{"style":1161}," row = 0; row \u003C dt.Rows.Count; row++) {",[39,1722,1723],{"style":1174},"                data[row + 1, col] = dt.Rows[row].ItemArray[col];",[39,1725,1726],{"style":1158},"            }",[39,1728,1699],{"style":1174},[39,1730,629],{"style":1158},[39,1732,1664,1733],{"style":1174},[47,1734,1735],{"style":1200},"\u002F\u002F Calculate the final column letter",[39,1737,1664,1738,1740,1741,1519],{"style":1158},[47,1739,1244],{"style":1161}," finalColLetter = ",[47,1742,1244],{"style":1161},[39,1744,1664,1745,1747,1748,1180],{"style":1174},[47,1746,1244],{"style":1161}," colCharset = ",[47,1749,1750],{"style":1178},"\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"",[39,1752,1664,1753,1755],{"style":1158},[47,1754,1576],{"style":1161}," colCharsetLen = colCharset.Length;",[39,1757,629],{"style":1174},[39,1759,1664,1760,1763],{"style":1158},[47,1761,1762],{"style":1161},"if"," (dt.Columns.Count > colCharsetLen) {",[39,1765,1766],{"style":1174},"            finalColLetter = colCharset.Substring(",[39,1768,1769],{"style":1158},"                (dt.Columns.Count - 1) \u002F colCharsetLen - 1, 1);",[39,1771,1699],{"style":1174},[39,1773,629],{"style":1158},[39,1775,1776],{"style":1174},"        finalColLetter += colCharset.Substring(",[39,1778,1779],{"style":1158},"                (dt.Columns.Count - 1) % colCharsetLen, 1);",[39,1781,629],{"style":1174},[39,1783,1664,1784],{"style":1158},[47,1785,1786],{"style":1200},"\u002F\u002F Create a new Sheet",[39,1788,1789],{"style":1174},"        Worksheet excelSheet = (Worksheet)excelWorkbook.Sheets.Add(excelWorkbook.Sheets.get_Item(++sheetIndex),",[39,1791,1792],{"style":1158},"                                                                    Type.Missing, 1, XlSheetType.xlWorksheet);",[39,1794,629],{"style":1174},[39,1796,1797,1798],{"style":1158},"        excelSheet.Name = dt.TableName; ",[47,1799,1800],{"style":1200},"\u002F\u002F name new sheet name of table",[39,1802,629],{"style":1174},[39,1804,1664,1805],{"style":1158},[47,1806,1807],{"style":1200},"\u002F\u002F Fast data export to Excel",[39,1809,1664,1810,1812,1813,1815,1816,1819],{"style":1174},[47,1811,1244],{"style":1161}," excelRange = ",[47,1814,1244],{"style":1161},".Format(",[47,1817,1818],{"style":1178},"\"A1:{0}{1}\"",", finalColLetter, dt.Rows.Count + 1);",[39,1821,1822],{"style":1158},"        excelSheet.get_Range(excelRange, Type.Missing).Value2 = data;",[39,1824,629],{"style":1174},[39,1826,1664,1827],{"style":1158},[47,1828,1829],{"style":1200},"\u002F\u002F Mark the first row as BOLD",[39,1831,1832,1833,1180],{"style":1174},"        ((Range)excelSheet.Rows[1, Type.Missing]).Font.Bold = ",[47,1834,1835],{"style":1161},"true",[39,1837,1328],{"style":1158},[39,1839,1263],{"style":1174},[1148,1841,894,1842,894],{"id":1150,"style":1151},[1148,1843,894,1844,894,894,1848,894,894,1853,894,894,1857,894,894,1862,894,894,1867,894,894,1874,894,894,1880,894,894,1883,894,894,1886,894,894,1888,894,894,1893,894,894,1898,894,894,1905,894,1163,894,1909,894,894,1912,894,894,1920,894,894,1923,894,894,1926,894,894,1928,894,894,1930,894,894,1933,894,894,1940,894,894,1943,894,894,1946,894,894,1953,894,894,1956,894,894,1959,894,894,1962,894,894,1964,894,894,1969,894,894,1971,894,894,1982,894,894,1984,894,894,1989,894],{"id":1154,"style":1155},[39,1845,1846],{"style":1158},[47,1847,1272],{"style":1200},[39,1849,1850],{"style":1174},[47,1851,1852],{"style":1200},"\u002F\u002F\u002F inserts data into excel row by row",[39,1854,1855],{"style":1158},[47,1856,1282],{"style":1200},[39,1858,1859],{"style":1174},[47,1860,1861],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"excel\">\u003C\u002Fparam>",[39,1863,1864],{"style":1158},[47,1865,1866],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"ds\">\u003C\u002Fparam>",[39,1868,1869,1163,1871,1873],{"style":1174},[47,1870,1162],{"style":1161},[47,1872,1166],{"style":1161}," InsertIntoExcel(Microsoft.Office.Interop.Excel.Application excel, System.Data.DataSet ds) {",[39,1875,1197,1876,1582,1878,1585],{"style":1158},[47,1877,1448],{"style":1161},[47,1879,1452],{"style":1161},[39,1881,1882],{"style":1174},"        Microsoft.Office.Interop.Excel.Worksheet theSheet = (Microsoft.Office.Interop.Excel.Worksheet)excel.Workbooks[1].Worksheets.Add(Type.Missing, Type.Missing, Type.Missing, Type.Missing);",[39,1884,1885],{"style":1158},"        theSheet.Name = dt.TableName;",[39,1887,629],{"style":1174},[39,1889,1664,1890,1892],{"style":1158},[47,1891,1576],{"style":1161}," colIndex = 0;",[39,1894,1664,1895,1897],{"style":1174},[47,1896,1576],{"style":1161}," rowIndex = 1;",[39,1899,1664,1900,1902,1903,1519],{"style":1158},[47,1901,1244],{"style":1161}," err = ",[47,1904,1244],{"style":1161},[39,1906,1664,1907,1303],{"style":1174},[47,1908,1302],{"style":1161},[39,1910,1911],{"style":1174},"            colIndex = 0;",[39,1913,1715,1914,1916,1917,1919],{"style":1158},[47,1915,1448],{"style":1161}," (DataColumn col ",[47,1918,1452],{"style":1161}," dt.Columns) {",[39,1921,1922],{"style":1174},"                colIndex += 1;",[39,1924,1925],{"style":1158},"                excel.Cells[1, colIndex] = col.ColumnName;",[39,1927,1726],{"style":1174},[39,1929,629],{"style":1158},[39,1931,1932],{"style":1174},"            rowIndex = 1;",[39,1934,1715,1935,1449,1937,1939],{"style":1158},[47,1936,1448],{"style":1161},[47,1938,1452],{"style":1161}," dt.Rows) {",[39,1941,1942],{"style":1174},"                rowIndex += 1;",[39,1944,1945],{"style":1158},"                colIndex = 0;",[39,1947,1948,1949,1916,1951,1919],{"style":1174},"                ",[47,1950,1448],{"style":1161},[47,1952,1452],{"style":1161},[39,1954,1955],{"style":1158},"                    colIndex += 1;",[39,1957,1958],{"style":1174},"                    excel.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();",[39,1960,1961],{"style":1158},"                }",[39,1963,1726],{"style":1174},[39,1965,1966,1967,1322],{"style":1158},"        } ",[47,1968,1321],{"style":1161},[39,1970,629],{"style":1174},[39,1972,1973,1974,1977,1978,1981],{"style":1158},"            err += ",[47,1975,1976],{"style":1178},"\"RowIndex=\""," + rowIndex.ToString() + ",[47,1979,1980],{"style":1178},"\" ColIndex=\""," + colIndex.ToString();",[39,1983,1699],{"style":1174},[39,1985,1318,1986],{"style":1158},[47,1987,1988],{"style":1200},"\u002F\u002F next ",[39,1990,1263],{"style":1174},[1148,1992,894,1993,894],{"id":1150},[1148,1994,894,1996,894,894,2000,894,894,2005,894,894,2009,894,894,2014,894,894,2019,894,894,2029,894,894,2034,894,894,2037,894,894,2040,894,894,2043,894,894,2049,894,894,2054,894],{"id":1154,"style":1995},"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; height: 207px; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[39,1997,1998],{"style":1158},[47,1999,1272],{"style":1200},[39,2001,2002],{"style":1174},[47,2003,2004],{"style":1200},"\u002F\u002F\u002F Save workbook to file system",[39,2006,2007],{"style":1158},[47,2008,1282],{"style":1200},[39,2010,2011],{"style":1174},[47,2012,2013],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"excelWorkbook\">\u003C\u002Fparam>",[39,2015,2016],{"style":1158},[47,2017,2018],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"fileName\">\u003C\u002Fparam>",[39,2020,2021,1163,2023,2025,2026,2028],{"style":1174},[47,2022,1162],{"style":1161},[47,2024,1166],{"style":1161}," SaveDoc(Workbook excelWorkbook, ",[47,2027,1244],{"style":1161}," fileName) {",[39,2030,1197,2031],{"style":1158},[47,2032,2033],{"style":1200},"\u002F\u002F Save and Close the Workbook",[39,2035,2036],{"style":1174},"    excelWorkbook.SaveAs(fileName, XlFileFormat.xlWorkbookNormal, Type.Missing,",[39,2038,2039],{"style":1158},"        Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlExclusive,",[39,2041,2042],{"style":1174},"        Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);",[39,2044,2045,2046,2048],{"style":1158},"    excelWorkbook.Close(",[47,2047,1835],{"style":1161},", Type.Missing, Type.Missing);",[39,2050,2051,2052,1180],{"style":1174},"    excelWorkbook = ",[47,2053,1297],{"style":1161},[39,2055,1263],{"style":1158},[1148,2057,894,2058,894],{"id":1150},[1148,2059,894,2060,894,894,2064,894,894,2069,894,894,2073,894,894,2078,894,894,2085,894,894,2090,894,894,2093,894,894,2098,894,894,2100,894,894,2105,894,894,2108,894,894,2111,894],{"id":1154,"style":1995},[39,2061,2062],{"style":1158},[47,2063,1272],{"style":1200},[39,2065,2066],{"style":1174},[47,2067,2068],{"style":1200},"\u002F\u002F\u002F Clean up and close document",[39,2070,2071],{"style":1158},[47,2072,1282],{"style":1200},[39,2074,2075],{"style":1174},[47,2076,2077],{"style":1200},"\u002F\u002F\u002F \u003Cparam name=\"excelApp\">\u003C\u002Fparam>",[39,2079,2080,1163,2082,2084],{"style":1158},[47,2081,1162],{"style":1161},[47,2083,1166],{"style":1161}," CloseExcelDocument(ApplicationClass excelApp) {",[39,2086,1197,2087],{"style":1174},[47,2088,2089],{"style":1200},"\u002F\u002F Release the Application object",[39,2091,2092],{"style":1158},"    excelApp.Quit();",[39,2094,2095,2096,1180],{"style":1174},"    excelApp = ",[47,2097,1297],{"style":1161},[39,2099,629],{"style":1158},[39,2101,1197,2102],{"style":1174},[47,2103,2104],{"style":1200},"\u002F\u002F Collect the unreferenced objects",[39,2106,2107],{"style":1158},"    GC.Collect();",[39,2109,2110],{"style":1174},"    GC.WaitForPendingFinalizers();",[39,2112,1263],{"style":1158},[604,2114],{"style":2115,"src":2116,"alt":11},"display: none;","\u002Farticles\u002Fimages\u002Fexport.jpg",{"title":11,"searchDepth":12,"depth":12,"links":2118},[],"2015-04-20T08:07:16.2600000-04:00","How to migrate data to Excel.",{},"\u002Farticles\u002Fexport-sql-data-to-microsoft-excel-using-visual-studio-2008-c",{"title":1120,"description":2120},"articles\u002Fexport-sql-data-to-microsoft-excel-using-visual-studio-2008-c",[18],"WrTKgWupiJpRf38vBT1Ecbke8nc6PFnUO6TDGtQ-kK8",{"id":2128,"title":2129,"author":28,"body":2130,"createdAt":2409,"description":2410,"extension":15,"img":2405,"meta":2411,"navigation":19,"path":2412,"seo":2413,"stem":2414,"tags":2415,"updatedAt":2409,"__hash__":2416},"articles\u002Farticles\u002Fsql-coalesce-command-features.md","SQL COALESCE Command Features",{"type":8,"value":2131,"toc":2407},[2132,2135,2204,2207,2215,2247,2256,2400,2403],[32,2133,2134],{},"In order to concatenate strings (delimited by with a string) from multiple rows in a SQL Table to a single field the Coalesce command is the one to use.  Typically COALESCE is used to return a single field value which represents multiple rows concatenated by a string.",[1148,2136,894,2137,894],{"id":1150},[1148,2138,894,2140,894,894,2151,894,894,2153,894,894,2172,894,894,2185,894,894,2191,894,894,2197,894,894,2199,894],{"id":1154,"style":2139},"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; height: 122px; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[39,2141,2142,2146,2147,2150],{"style":1158},[47,2143,2145],{"style":2144},"color: #0000ff","DECLARE"," @EmployeeList ",[47,2148,2149],{"style":2144},"varchar","(100)",[39,2152,629],{"style":1174},[39,2154,2155,2158,2159,2162,2163,2167,2168,2171],{"style":1158},[47,2156,2157],{"style":2144},"SELECT"," @EmployeeList = ",[47,2160,2161],{"style":2144},"COALESCE","(@EmployeeList + ",[47,2164,2166],{"style":2165},"color: #006080","', '",", ",[47,2169,2170],{"style":2165},"''",") + ",[39,2173,2174,2175,2178,2179,1163,2182,2184],{"style":1174},"   ",[47,2176,2177],{"style":2144},"CAST","(Emp_UniqueID ",[47,2180,2181],{"style":2144},"AS",[47,2183,2149],{"style":2144},"(5))",[39,2186,2187,2190],{"style":1158},[47,2188,2189],{"style":2144},"FROM"," SalesCallsEmployees",[39,2192,2193,2196],{"style":1174},[47,2194,2195],{"style":2144},"WHERE"," SalCal_UniqueID = 1",[39,2198,629],{"style":1158},[39,2200,2201,2203],{"style":1174},[47,2202,2157],{"style":2144}," @EmployeeList",[459,2205],{"href":2206},"http:\u002F\u002F11011.net\u002Fsoftware\u002Fvspaste",[32,2208,2209,2210,2212,2213,424],{},"The output from the following would be something like 1,2, 3",[406,2211],{},"\nThe following example uses a function to return a joined table field with the primary data selected.  The challenge was to return a single row from one table while returning values from the joined table into a single field.  To accomplish this I used syntax similar to the following:",[406,2214],{},[39,2216,2218,2226,2230,2231,2233,2234,2237,2239,2242,2243,2246],{"className":2217},[44],[47,2219,2221,2222],{"style":2220},"color: blue","SELECT ",[47,2223,2225],{"style":2224},"color: #000000;","personID",[47,2227,2229],{"style":2228},"color: gray",",","dbo",[47,2232,364],{"style":2228},"fn_CombineValues",[47,2235,2236],{"style":2228},"(",[47,2238,2225],{"style":2224},[47,2240,2241],{"style":2228},") ","Roles  \n  ",[47,2244,2245],{"style":2220},"FROM ","[People]",[32,2248,2249,2251,1140,2253,2255],{},[459,2250],{"href":2206},[406,2252],{},[406,2254],{},"\nThe following function was created to support the above sql query.  The function accepts the key to be used in the the joined table and returns a string value representing in this case the roles related to the primary table.",[1148,2257,894,2258,894],{"id":1150},[1148,2259,894,2261,894,894,2270,894,894,2272,894,894,2303,894,894,2306,894,894,2315,894,894,2319,894,894,2324,894,894,2332,894,894,2334,894,894,2356,894,894,2361,894,894,2374,894,894,2381,894,894,2383,894,894,2388,894,894,2390,894,894,2395,894,894,2397,894],{"id":1154,"style":2260},"text-align: left; line-height: 12pt; background-color: #f4f4f4; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; height: 305px; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;",[39,2262,2263,1163,2266,2269],{"style":1158},[47,2264,2265],{"style":2144},"CREATE",[47,2267,2268],{"style":2144},"FUNCTION"," fn_CombineValues",[39,2271,1690],{"style":1174},[39,2273,2274,2275,2278,2279,1163,2282,1163,2285,2288,2289,2292,2293,1163,2296,1163,2299,2302],{"style":1158},"     @FK_ID ",[47,2276,2277],{"style":2144},"INT"," --The ",[47,2280,2281],{"style":2144},"foreign",[47,2283,2284],{"style":2144},"key",[47,2286,2287],{"style":2144},"from"," TableA which ",[47,2290,2291],{"style":2144},"is"," used ",[47,2294,2295],{"style":2144},"to",[47,2297,2298],{"style":2144},"fetch",[47,2300,2301],{"style":2144},"corresponding"," records",[39,2304,2305],{"style":1174}," )",[39,2307,1163,2308,1163,2311,2314],{"style":1158},[47,2309,2310],{"style":2144},"RETURNS",[47,2312,2313],{"style":2144},"VARCHAR","(8000)",[39,2316,1163,2317],{"style":1174},[47,2318,2181],{"style":2144},[39,2320,1163,2321],{"style":1158},[47,2322,2323],{"style":2144},"BEGIN",[39,2325,1163,2326,2328,2329,2331],{"style":1174},[47,2327,2145],{"style":2144}," @SomeColumnList ",[47,2330,2313],{"style":2144},"(8000);",[39,2333,629],{"style":1158},[39,2335,1163,2336,2338,2339,2341,2342,2167,2344,2171,2346,2236,2348,1163,2351,1163,2353,2355],{"style":1174},[47,2337,2157],{"style":2144}," @SomeColumnList = ",[47,2340,2161],{"style":2144},"(@SomeColumnList + ",[47,2343,2166],{"style":2165},[47,2345,2170],{"style":2165},[47,2347,2177],{"style":2144},[47,2349,2350],{"style":2144},"Role",[47,2352,2181],{"style":2144},[47,2354,2149],{"style":2144},"(20)) ",[39,2357,1163,2358,2360],{"style":1158},[47,2359,2189],{"style":2144}," dbo.SA_PeopleRoles C",[39,2362,1163,2363,1163,2366,2369,2370,2373],{"style":1174},[47,2364,2365],{"style":2144},"INNER",[47,2367,2368],{"style":2144},"JOIN"," dbo.SA_Roles r ",[47,2371,2372],{"style":2144},"ON"," r.roleid=c.RoleID",[39,2375,2377,2378,2380],{"style":2376},"text-align: left; line-height: 12pt; background-color: white; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size\u003Cmce: script type=;",": 8pt; overflow: visible; border-style: none; padding: 0px;\"> ",[47,2379,2195],{"style":2144}," C.personID = @FK_ID;",[39,2382,629],{"style":1174},[39,2384,1163,2385,1163],{"style":1158},[47,2386,2387],{"style":2144},"RETURN",[39,2389,1690],{"style":1174},[39,2391,1163,2392,2394],{"style":1158},[47,2393,2157],{"style":2144}," @SomeColumnList",[39,2396,2305],{"style":1174},[39,2398,2399],{"style":1158}," END",[459,2401,2402],{"href":2402},"http:\u002F\u002Fstackoverflow.com\u002Fquestions\u002F111341\u002Fcombine-multiple-results-in-a-subquery-into-a-single-comma-separated-value",[604,2404],{"src":2405,"alt":11,"style":2406},"\u002Farticles\u002Fimages\u002Fsql3.png","display:none;",{"title":11,"searchDepth":12,"depth":12,"links":2408},[],"2015-04-20T08:07:16.1500000-04:00","Concatenate strings within SQL",{},"\u002Farticles\u002Fsql-coalesce-command-features",{"title":2129,"description":2410},"articles\u002Fsql-coalesce-command-features",[18],"Lvcvf-IInMfPOb1Xmt4Hkny-iQtpdEQ4KhdeRiK6CKo",{"id":2418,"title":2419,"author":28,"body":2420,"createdAt":2851,"description":335,"extension":15,"img":2852,"meta":2853,"navigation":19,"path":2854,"seo":2855,"stem":2856,"tags":2857,"updatedAt":2851,"__hash__":2858},"articles\u002Farticles\u002Fsql-server-and-drop-all-objects-tables-views-stored-procedures-etc.md","SQL Server and Drop all Objects (Tables\u002FViews\u002FStored Procedures etc.)",{"type":8,"value":2421,"toc":2849},[2422,2425,2847],[32,2423,2424],{},"The following SQL will drop all SQL Server objects within a database.",[39,2426,2428],{"className":41,"code":2427,"language":18,"meta":11,"style":11},"\u002F* Drop all non-system stored procs *\u002F \nDECLARE @name VARCHAR(128)    \nDECLARE @SQL VARCHAR(254)    \nSELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])    \nWHILE @name is not null    \nBEGIN    \n    SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'    \n    EXEC (@SQL)    \n    PRINT 'Dropped Procedure: ' + @name    \n    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] &gt; @name ORDER BY [name])    \nEND    \nGO  \n\u002F* Drop all views *\u002F     \nDECLARE @name VARCHAR(128)    \nDECLARE @SQL VARCHAR(254)    \nSELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])    \nWHILE @name IS NOT NULL    \nBEGIN    \n    SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'    \n    EXEC (@SQL)    \n    PRINT 'Dropped View: ' + @name    \n    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] &gt; @name ORDER BY [name])    \nEND    \nGO  \n\u002F* Drop all functions *\u002F     \nDECLARE @name VARCHAR(128)    \nDECLARE @SQL VARCHAR(254)    \nSELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])    \nWHILE @name IS NOT NULL    \nBEGIN    \n    SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'    \n    EXEC (@SQL)    \n    PRINT 'Dropped Function: ' + @name    \n    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] &gt; @name ORDER BY [name])    \nEND    \nGO  \n\u002F* Drop all Foreign Key constraints *\u002F     \nDECLARE @name VARCHAR(128)    \nDECLARE @constraint VARCHAR(254)    \nDECLARE @SQL VARCHAR(254)    \nSELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)    \nWHILE @name is not null    \nBEGIN    \n    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n    WHILE @constraint IS NOT NULL    \n    BEGIN    \n        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)    \n        EXEC (@SQL)    \n        PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name    \n        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME &lt;&gt; @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n    END    \nSELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)    \nEND    \nGO  \n\u002F* Drop all Primary Key constraints *\u002F     \nDECLARE @name VARCHAR(128)    \nDECLARE @constraint VARCHAR(254)    \nDECLARE @SQL VARCHAR(254)    \nSELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)    \nWHILE @name IS NOT NULL    \nBEGIN    \n    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n    WHILE @constraint is not null    \n    BEGIN    \n        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)    \n        EXEC (@SQL)    \n        PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name    \n        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME &lt;&gt; @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n    END    \nSELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)    \nEND    \nGO  \n\u002F* Drop all tables *\u002F     \nDECLARE @name VARCHAR(128)    \nDECLARE @SQL VARCHAR(254)    \nSELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])    \nWHILE @name IS NOT NULL    \nBEGIN    \n    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'    \n    EXEC (@SQL)    \n    PRINT 'Dropped Table: ' + @name    \nSELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] &gt; @name ORDER BY [name])    \nEND    \nGO\n",[44,2429,2430,2435,2440,2445,2450,2455,2460,2465,2470,2475,2480,2485,2490,2495,2499,2503,2508,2513,2517,2522,2526,2531,2536,2540,2544,2549,2553,2557,2562,2566,2570,2575,2579,2584,2589,2593,2597,2602,2606,2611,2615,2620,2624,2628,2633,2638,2643,2648,2653,2659,2665,2671,2676,2681,2686,2692,2697,2702,2707,2713,2718,2723,2729,2735,2740,2745,2750,2756,2762,2767,2772,2777,2782,2788,2793,2798,2804,2809,2814,2820,2825,2831,2837,2842],{"__ignoreMap":11},[47,2431,2432],{"class":49,"line":50},[47,2433,2434],{},"\u002F* Drop all non-system stored procs *\u002F \n",[47,2436,2437],{"class":49,"line":12},[47,2438,2439],{},"DECLARE @name VARCHAR(128)    \n",[47,2441,2442],{"class":49,"line":61},[47,2443,2444],{},"DECLARE @SQL VARCHAR(254)    \n",[47,2446,2447],{"class":49,"line":67},[47,2448,2449],{},"SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 ORDER BY [name])    \n",[47,2451,2452],{"class":49,"line":73},[47,2453,2454],{},"WHILE @name is not null    \n",[47,2456,2457],{"class":49,"line":79},[47,2458,2459],{},"BEGIN    \n",[47,2461,2462],{"class":49,"line":84},[47,2463,2464],{},"    SELECT @SQL = 'DROP PROCEDURE [dbo].[' + RTRIM(@name) +']'    \n",[47,2466,2467],{"class":49,"line":90},[47,2468,2469],{},"    EXEC (@SQL)    \n",[47,2471,2472],{"class":49,"line":95},[47,2473,2474],{},"    PRINT 'Dropped Procedure: ' + @name    \n",[47,2476,2477],{"class":49,"line":101},[47,2478,2479],{},"    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'P' AND category = 0 AND [name] &gt; @name ORDER BY [name])    \n",[47,2481,2482],{"class":49,"line":107},[47,2483,2484],{},"END    \n",[47,2486,2487],{"class":49,"line":112},[47,2488,2489],{},"GO  \n",[47,2491,2492],{"class":49,"line":118},[47,2493,2494],{},"\u002F* Drop all views *\u002F     \n",[47,2496,2497],{"class":49,"line":123},[47,2498,2439],{},[47,2500,2501],{"class":49,"line":129},[47,2502,2444],{},[47,2504,2505],{"class":49,"line":135},[47,2506,2507],{},"SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 ORDER BY [name])    \n",[47,2509,2510],{"class":49,"line":141},[47,2511,2512],{},"WHILE @name IS NOT NULL    \n",[47,2514,2515],{"class":49,"line":147},[47,2516,2459],{},[47,2518,2519],{"class":49,"line":152},[47,2520,2521],{},"    SELECT @SQL = 'DROP VIEW [dbo].[' + RTRIM(@name) +']'    \n",[47,2523,2524],{"class":49,"line":158},[47,2525,2469],{},[47,2527,2528],{"class":49,"line":163},[47,2529,2530],{},"    PRINT 'Dropped View: ' + @name    \n",[47,2532,2533],{"class":49,"line":169},[47,2534,2535],{},"    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'V' AND category = 0 AND [name] &gt; @name ORDER BY [name])    \n",[47,2537,2538],{"class":49,"line":175},[47,2539,2484],{},[47,2541,2542],{"class":49,"line":180},[47,2543,2489],{},[47,2545,2546],{"class":49,"line":186},[47,2547,2548],{},"\u002F* Drop all functions *\u002F     \n",[47,2550,2551],{"class":49,"line":192},[47,2552,2439],{},[47,2554,2555],{"class":49,"line":198},[47,2556,2444],{},[47,2558,2559],{"class":49,"line":204},[47,2560,2561],{},"SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 ORDER BY [name])    \n",[47,2563,2564],{"class":49,"line":210},[47,2565,2512],{},[47,2567,2568],{"class":49,"line":215},[47,2569,2459],{},[47,2571,2572],{"class":49,"line":221},[47,2573,2574],{},"    SELECT @SQL = 'DROP FUNCTION [dbo].[' + RTRIM(@name) +']'    \n",[47,2576,2577],{"class":49,"line":227},[47,2578,2469],{},[47,2580,2581],{"class":49,"line":233},[47,2582,2583],{},"    PRINT 'Dropped Function: ' + @name    \n",[47,2585,2586],{"class":49,"line":238},[47,2587,2588],{},"    SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] IN (N'FN', N'IF', N'TF', N'FS', N'FT') AND category = 0 AND [name] &gt; @name ORDER BY [name])    \n",[47,2590,2591],{"class":49,"line":244},[47,2592,2484],{},[47,2594,2595],{"class":49,"line":250},[47,2596,2489],{},[47,2598,2599],{"class":49,"line":255},[47,2600,2601],{},"\u002F* Drop all Foreign Key constraints *\u002F     \n",[47,2603,2604],{"class":49,"line":260},[47,2605,2439],{},[47,2607,2608],{"class":49,"line":266},[47,2609,2610],{},"DECLARE @constraint VARCHAR(254)    \n",[47,2612,2613],{"class":49,"line":271},[47,2614,2444],{},[47,2616,2617],{"class":49,"line":276},[47,2618,2619],{},"SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' ORDER BY TABLE_NAME)    \n",[47,2621,2622],{"class":49,"line":282},[47,2623,2454],{},[47,2625,2626],{"class":49,"line":288},[47,2627,2459],{},[47,2629,2630],{"class":49,"line":294},[47,2631,2632],{},"    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n",[47,2634,2635],{"class":49,"line":299},[47,2636,2637],{},"    WHILE @constraint IS NOT NULL    \n",[47,2639,2640],{"class":49,"line":304},[47,2641,2642],{},"    BEGIN    \n",[47,2644,2645],{"class":49,"line":309},[47,2646,2647],{},"        SELECT @SQL = 'ALTER TABLE [dbo].[' + RTRIM(@name) +'] DROP CONSTRAINT ' + RTRIM(@constraint)    \n",[47,2649,2650],{"class":49,"line":314},[47,2651,2652],{},"        EXEC (@SQL)    \n",[47,2654,2656],{"class":49,"line":2655},49,[47,2657,2658],{},"        PRINT 'Dropped FK Constraint: ' + @constraint + ' on ' + @name    \n",[47,2660,2662],{"class":49,"line":2661},50,[47,2663,2664],{},"        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'FOREIGN KEY' AND CONSTRAINT_NAME &lt;&gt; @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n",[47,2666,2668],{"class":49,"line":2667},51,[47,2669,2670],{},"    END    \n",[47,2672,2674],{"class":49,"line":2673},52,[47,2675,2619],{},[47,2677,2679],{"class":49,"line":2678},53,[47,2680,2484],{},[47,2682,2684],{"class":49,"line":2683},54,[47,2685,2489],{},[47,2687,2689],{"class":49,"line":2688},55,[47,2690,2691],{},"\u002F* Drop all Primary Key constraints *\u002F     \n",[47,2693,2695],{"class":49,"line":2694},56,[47,2696,2439],{},[47,2698,2700],{"class":49,"line":2699},57,[47,2701,2610],{},[47,2703,2705],{"class":49,"line":2704},58,[47,2706,2444],{},[47,2708,2710],{"class":49,"line":2709},59,[47,2711,2712],{},"SELECT @name = (SELECT TOP 1 TABLE_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' ORDER BY TABLE_NAME)    \n",[47,2714,2716],{"class":49,"line":2715},60,[47,2717,2512],{},[47,2719,2721],{"class":49,"line":2720},61,[47,2722,2459],{},[47,2724,2726],{"class":49,"line":2725},62,[47,2727,2728],{},"    SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n",[47,2730,2732],{"class":49,"line":2731},63,[47,2733,2734],{},"    WHILE @constraint is not null    \n",[47,2736,2738],{"class":49,"line":2737},64,[47,2739,2642],{},[47,2741,2743],{"class":49,"line":2742},65,[47,2744,2647],{},[47,2746,2748],{"class":49,"line":2747},66,[47,2749,2652],{},[47,2751,2753],{"class":49,"line":2752},67,[47,2754,2755],{},"        PRINT 'Dropped PK Constraint: ' + @constraint + ' on ' + @name    \n",[47,2757,2759],{"class":49,"line":2758},68,[47,2760,2761],{},"        SELECT @constraint = (SELECT TOP 1 CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE constraint_catalog=DB_NAME() AND CONSTRAINT_TYPE = 'PRIMARY KEY' AND CONSTRAINT_NAME &lt;&gt; @constraint AND TABLE_NAME = @name ORDER BY CONSTRAINT_NAME)    \n",[47,2763,2765],{"class":49,"line":2764},69,[47,2766,2670],{},[47,2768,2770],{"class":49,"line":2769},70,[47,2771,2712],{},[47,2773,2775],{"class":49,"line":2774},71,[47,2776,2484],{},[47,2778,2780],{"class":49,"line":2779},72,[47,2781,2489],{},[47,2783,2785],{"class":49,"line":2784},73,[47,2786,2787],{},"\u002F* Drop all tables *\u002F     \n",[47,2789,2791],{"class":49,"line":2790},74,[47,2792,2439],{},[47,2794,2796],{"class":49,"line":2795},75,[47,2797,2444],{},[47,2799,2801],{"class":49,"line":2800},76,[47,2802,2803],{},"SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 ORDER BY [name])    \n",[47,2805,2807],{"class":49,"line":2806},77,[47,2808,2512],{},[47,2810,2812],{"class":49,"line":2811},78,[47,2813,2459],{},[47,2815,2817],{"class":49,"line":2816},79,[47,2818,2819],{},"    SELECT @SQL = 'DROP TABLE [dbo].[' + RTRIM(@name) +']'    \n",[47,2821,2823],{"class":49,"line":2822},80,[47,2824,2469],{},[47,2826,2828],{"class":49,"line":2827},81,[47,2829,2830],{},"    PRINT 'Dropped Table: ' + @name    \n",[47,2832,2834],{"class":49,"line":2833},82,[47,2835,2836],{},"SELECT @name = (SELECT TOP 1 [name] FROM sysobjects WHERE [type] = 'U' AND category = 0 AND [name] &gt; @name ORDER BY [name])    \n",[47,2838,2840],{"class":49,"line":2839},83,[47,2841,2484],{},[47,2843,2845],{"class":49,"line":2844},84,[47,2846,836],{},[318,2848,320],{},{"title":11,"searchDepth":12,"depth":12,"links":2850},[],"2015-04-20T08:07:15.3400000-04:00","\u002Farticles\u002Fimages\u002Fsql.jpg",{},"\u002Farticles\u002Fsql-server-and-drop-all-objects-tables-views-stored-procedures-etc",{"title":2419,"description":335},"articles\u002Fsql-server-and-drop-all-objects-tables-views-stored-procedures-etc",[18,554],"-AosXGHvi-Nm2hxkKypyQPWlfVYZ_LUe68mREC3CbRY",{"id":2860,"title":2861,"author":28,"body":2862,"createdAt":2916,"description":2917,"extension":15,"img":2918,"meta":2919,"navigation":19,"path":2920,"seo":2921,"stem":2922,"tags":2923,"updatedAt":2916,"__hash__":2925},"articles\u002Farticles\u002Fnew-ssdt-power-tools-now-for-both-visual-studio-2010-and-visual-studio-2012.md","New SSDT Power Tools! Now for both Visual Studio 2010 and Visual Studio 2012",{"type":8,"value":2863,"toc":2914},[2864,2867,2870],[32,2865,2866],{},"Microsoft is pleased to announce the latest release of SSDT Power Tools!",[32,2868,2869],{},"We continue to use power tools to get early versions of experiences or quick features to you and we’re always interested in hearing your feedback.",[32,2871,2872,2873,2877,2878,2881,2885,2886,2889,2892,2893,2897,2898,2902,2903,2905,2906,2908,2911],{},"This release of the tools (Version 1.3) builds on the previous release.  This release of the power tools is",[2874,2875,2876],"i",{}," only ","compatible with the newest update for SQL Server Data Tools. First, get the SSDT – September 2012 update for Visual Studio 2010 or Visual Studio 2012 here:  SSDT for Visual Studio 2012: ",[459,2879],{"href":2880},"http:\u002F\u002Fmsdn.microsoft.com\u002Fen-us\u002Fjj650015",[459,2882,2880],{"href":2880,"rel":2883},[2884],"nofollow","  SSDT for Visual Studio 2010: ",[459,2887],{"href":2888},"http:\u002F\u002Fmsdn.microsoft.com\u002Fen-us\u002Fjj650014",[459,2890,2888],{"href":2888,"rel":2891},[2884],"    For the first time, this power tools release provides a version of the power tools for Visual Studio 2012 in addition to the version for Visual Studio 2010. They are separate installs that you can grab here:  ",[459,2894,2896],{"href":2895},"http:\u002F\u002Fvisualstudiogallery.msdn.microsoft.com\u002F9b0228c6-15d1-44de-9279-66dde12bf861?SRC=Featured","SSDT Power Tools for Visual Studio 2010","  ",[459,2899,2901],{"href":2900},"http:\u002F\u002Fvisualstudiogallery.msdn.microsoft.com\u002F96a2f8cc-0c8b-47dd-93cd-1e8e9f34a917","SSDT Power Tools for Visual Studio 2012","  Requirements:",[406,2904],{},"\nSQL Server 2008 Service Pack 1",[406,2907],{},[459,2909],{"href":2910},"http:\u002F\u002Fwww.microsoft.com\u002Fen-us\u002Fdownload\u002Fdetails.aspx?id=20302",[459,2912,2910],{"href":2910,"rel":2913},[2884],{"title":11,"searchDepth":12,"depth":12,"links":2915},[],"2015-04-20T08:07:12.8400000-04:00","SQL Server Data Tools (SSDT) provides project templates and design surfaces for building SQL Server content types - relational databases, Analysis Services models, Reporting Services reports, and Integration Services packages.","\u002Farticles\u002Fimages\u002FMdpbTnH58m.png",{},"\u002Farticles\u002Fnew-ssdt-power-tools-now-for-both-visual-studio-2010-and-visual-studio-2012",{"title":2861,"description":2917},"articles\u002Fnew-ssdt-power-tools-now-for-both-visual-studio-2010-and-visual-studio-2012",[2924,18,554],"visualstudio","e_YUJhN16WAmwBhand1Scn-44WrB72gU7EuktLScjHE",1781574760364]