{"id":85,"date":"2023-03-24T15:45:24","date_gmt":"2023-03-24T13:45:24","guid":{"rendered":"http:\/\/130.61.57.200\/?p=85"},"modified":"2023-04-12T11:18:43","modified_gmt":"2023-04-12T09:18:43","slug":"generic-export-to-csv-script","status":"publish","type":"post","link":"http:\/\/130.61.57.200\/index.php\/2023\/03\/24\/generic-export-to-csv-script\/","title":{"rendered":"Generic Export to csv script"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A generic way to export a table to csv.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>create table exportLog (\nid int identity(1,1) Primary key, \ntablename varchar(100), \nexportDate datetime2,\nexportColumn varchar (100),\nexportFrom varchar(200),\nexportTo varchar(200),\nexportRows int,\nexportJob varchar (100),\nexportFile varchar(300)\n)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/*\nProcedure exports a csv file with multiple variables:\nexport file name: @filename (e.g. export.csv)\npath:\t\t\t  @exportPath (e.g. c:\\export)\nTable to export:  @exportTable\nexport filters\nFilter rows based on one column: @exportColumn\nUse &gt; and &lt;=: @exportFrom,@exportTo\n*\/\ndeclare\n@serverName varchar (100),\n@exportTable varchar (50)=&#039;Shop.dbo.Test&#039;,\n@exportColumn varchar(100) =&#039;c4&#039;,\n@exportColumnDataType varchar(50),\n@exportFrom varchar (100)=&#039;&#039;,\n@exportTo varchar(100)=&#039;&#039;,\n@cmdshellValue sql_variant,\n@exportPath varchar(500)=&#039;c:\\export&#039;,\n@filename varchar(100),\n@sql nvarchar (2000),\n@rows int ;\n\nBegin\n--Initialize Parameters\nselect @filename=&#039;Person_&#039;+format(getdate(),&#039;yyyyMMddHHmmss&#039;)+&#039;.csv&#039;\nselect @serverName=cast(serverproperty(&#039;ServerName&#039;) as varchar);\nselect @exportColumnDataType=t.name from sys.all_columns ac \njoin sys.types t on t.system_type_id=ac.system_type_id\nwhere object_id=OBJECT_ID(@exportTable) and ac.name=@exportColumn;\n\n\n\n---Check xp_cmdshell condition\nEXEC sp_configure &#039;show advanced options&#039;, 1\nRECONFIGURE WITH OVERRIDE;\nselect @cmdshellValue =value_in_use  from sys.configurations where name =&#039;xp_cmdshell&#039;;\nif @cmdshellValue=0 \nBegin\nEXEC sp_configure &#039;xp_cmdshell&#039;, 1\nRECONFIGURE WITH OVERRIDE;\nend;\n\n-------------------------------\n\/* When export column is set but From - To boundaries are not set then export uses max value loged in export log table as From and max current value on this column\n*\/\nif (@exportColumn!=&#039;ALL&#039; AND (@exportFrom =&#039;&#039; or @exportTo =&#039;&#039;))\nbegin\nif (@exportFrom =&#039;&#039; )\nbegin\nselect @exportFrom=max(exportTo) from exportLog where tablename=@exportTable;\nend;\nif (@exportTo =&#039;&#039;)\nbegin\nif @exportColumnDataType=&#039;datetime&#039;\nselect @sql= &#039;select @ExportTo=max(cast(cast (&#039;+@exportColumn+&#039; as datetime2) as varchar)) from &#039;+@exportTable+&#039;;&#039;;\nelse\nselect @sql= &#039;select @ExportTo=max(&#039;+@exportColumn+&#039;) from &#039;+@exportTable+&#039;;&#039;;\n\nprint @sql;\nexec sp_executesql @sql,N&#039;@ExportTo varchar(100) output&#039;,@ExportTo OUTPUT;\nend;\nend;\n\n\n----Export\nif @exportColumnDataType=&#039;datetime&#039;\nselect @sql= &#039;select @rows=count(*) from &#039;+@exportTable+case when @exportColumn=&#039;ALL&#039; then &#039;&#039; else &#039; where &#039;+@exportColumn  +&#039;  &gt; cast(&#039;&#039;&#039;+@exportFrom+&#039;&#039;&#039; as datetime2) AND &#039;+@exportColumn  + &#039;&lt;=cast(&#039;&#039;&#039;+@exportTo+&#039;&#039;&#039; as datetime2) &#039; end +&#039;;&#039;\nelse\nselect @sql= &#039;select @rows=count(*) from &#039;+@exportTable+case when @exportColumn=&#039;ALL&#039; then &#039;&#039; else &#039; where &#039;+@exportColumn  +&#039;  &gt; &#039;&#039;&#039;+@exportFrom+&#039;&#039;&#039; AND &#039;+@exportColumn  + &#039;&lt;=&#039;&#039;&#039;+@exportTo+&#039;&#039;&#039;&#039; end +&#039;;&#039;\nprint @sql;\nexec sp_executesql @sql,N&#039;@rows int output&#039;,@rows OUTPUT;\nprint @rows\n\nif @exportColumnDataType=&#039;datetime&#039;\nselect @sql=&#039;EXEC  master..xp_cmdshell &#039;&#039;SQLCMD -S &#039;+@serverName+&#039; -E -Q &quot;SET NOCOUNT ON; SELECT * FROM &#039;+@exportTable+case when @exportColumn=&#039;ALL&#039; then &#039;&#039; else &#039; where &#039;+@exportColumn  +&#039;  between cast (&#039;&#039;&#039;&#039;&#039;+@exportFrom+&#039;&#039;&#039;&#039;&#039;as datetime2) AND cast (&#039;+&#039;&#039;&#039;&#039;&#039;&#039;+@exportTo +&#039;&#039;&#039;&#039;&#039;&#039; end +&#039; as datetime2); &quot; -r 0 -b -o &#039;+@exportPath+&#039;\\&#039;+@filename+&#039;&#039;&#039;&#039;;\nelse\nselect @sql=&#039;EXEC  master..xp_cmdshell &#039;&#039;SQLCMD -S &#039;+@serverName+&#039; -E -Q &quot;SET NOCOUNT ON; SELECT * FROM &#039;+@exportTable+case when @exportColumn=&#039;ALL&#039; then &#039;&#039; else &#039; where &#039;+@exportColumn  +&#039;  between &#039;&#039;&#039;&#039;&#039;+@exportFrom+&#039;&#039;&#039;&#039;&#039; AND &#039;+&#039;&#039;&#039;&#039;&#039;&#039;+@exportTo +&#039;&#039;&#039;&#039;&#039;&#039; end +&#039;; &quot; -r 0 -b -o &#039;+@exportPath+&#039;\\&#039;+@filename+&#039;&#039;&#039;&#039;;\n\nprint @sql;\nexec sp_executesql @sql;\n\n\n\n----Log to table\ninsert into exportLog (tablename,exportDate,exportColumn,exportFrom, exportTo ,exportRows,exportJob, exportFile ) values (@exportTable,sysdatetime(),@exportColumn,@exportFrom,@exportTo,@rows,&#039;Export &#039;+@exportTable,@exportPath+&#039;\\&#039;+@filename)\n\n\nif @cmdshellValue=0 \nbegin\nEXEC sp_configure &#039;xp_cmdshell&#039;, 0;\nRECONFIGURE WITH OVERRIDE;\nend;\nEXEC sp_configure &#039;show advanced options&#039;, 0;\nRECONFIGURE WITH OVERRIDE;\nEnd;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A generic way to export a table to csv.<\/p>\n","protected":false},"author":1,"featured_media":104,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-mssql"],"blocksy_meta":[],"_links":{"self":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":6,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"predecessor-version":[{"id":103,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/posts\/85\/revisions\/103"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/media\/104"}],"wp:attachment":[{"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/130.61.57.200\/index.php\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}