Comments for SQLSERVERLEARNER https://sqlserverlearner.com LEARN SQL SERVER ONLINE Wed, 15 Jul 2015 13:54:21 +0000 hourly 1 https://wordpress.org/?v=6.5.2 Comment on HOW TO CONVERT TABLE DATA TO XML AND XML TO TABLE by Bitsqueezer https://sqlserverlearner.com/2013/09/28/how-to-convert-table-data-to-xml-and-vice-versa/comment-page-1/#comment-2172 Wed, 15 Jul 2015 13:54:21 +0000 https://sqlserverlearner.azurewebsites.net/2013/09/28/how-to-convert-table-data-to-xml-and-vice-versa/#comment-2172 Thanks for this method, works great, also with views instead of tables.

I’ve made two stored procedures from this to use that with every table/view in the database, maybe useful for other readers of your blog:

— ===================================================================================================================================
— Author: Devi Prasad / Christian Coppes
— Original from: http://sqlserverlearner.com/2013/how-to-convert-table-data-to-xml-and-vice-versa
— Create date: 15.07.2015
— Last Change: 15.07.2015
— Description: Creates XML strings (schema and data) from a specified table
— ===================================================================================================================================

CREATE PROCEDURE dbo.procConvertTableToXML
(
@strTableASnvarchar(128),– Name of the table to convert
@strSchemaASnvarchar(128),– Schema name of the table
@xmlTableSchemaASxml OUTPUT,– Return value of the table schema xml
@xmlTableDataASxml OUTPUT,– Return value of the table data xml
@strFilterASnvarchar(MAX) = ”– optional WHERE string
)
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE @intReturnValue ASint = 0;
DECLARE @strSQLASnvarchar(MAX);

BEGIN TRY
SELECT @xmlTableSchema = (SELECT C.COLUMN_NAME, C.DATA_TYPE,
CASE (C.IS_NULLABLE)
WHEN ‘YES’ THEN ‘true’
ELSE ‘false’
END AS IS_NULLABLE,
(CASE C.CHARACTER_MAXIMUM_LENGTH
WHEN -1 THEN ‘MAX’
ELSE CAST(C.CHARACTER_MAXIMUM_LENGTH AS nvarchar(10)) END) AS CHARLENGTH
FROM INFORMATION_SCHEMA.COLUMNS AS C
WHERE C.TABLE_NAME = @strTable
AND C.TABLE_SCHEMA = @strSchema
FOR XML AUTO, ROOT(‘Table’)
);

— SELECT * is OK here as this is dynamic SQL
SET @strSQL = ‘SET @xmlTableData = (SELECT TableRow.*
FROM [‘ + @strSchema + ‘].[‘ + @strTable + ‘] AS TableRow’
+ (CASE WHEN ISNULL(@strFilter,”) = ” THEN ” ELSE ‘ WHERE ‘ + @strFilter END) +
‘ FOR XML AUTO, BINARY BASE64, ROOT(”TableData”))’;

EXEC sys.sp_executesql @strSQL, N’@xmlTableData xml OUTPUT’,@xmlTableData = @xmlTableData OUTPUT;
END TRY
BEGIN CATCH
RETURN -1;
END CATCH

LabelExit:
RETURN @intReturnValue;

END

—————————————

— ===================================================================================================================================
— Author: Devi Prasad / Christian Coppes
— Original from: http://sqlserverlearner.com/2013/how-to-convert-table-data-to-xml-and-vice-versa
— Create date: 15.07.2015
— Last Change: 15.07.2015
— Description: Creates XML strings (schema and data) from a specified table
— ===================================================================================================================================

CREATE PROCEDURE dbo.procConvertXMLToTable
(
@xmlTableSchemaASxml,– XML string with the table schema created by procConvertTableToXML
@xmlTableDataASxml,– XML string with the table data created by procConvertTableToXML
@strFilterASnvarchar(MAX) = ”– optional WHERE string
)
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON;

DECLARE @intReturnValue ASint = 0;
DECLARE @strSQLASnvarchar(MAX) = ”;

BEGIN TRY

IF OBJECT_ID(‘tempdb..#tblXMLColumns’) IS NOT NULL
DROP TABLE #tblXMLColumns;

SELECT x.value(‘@COLUMN_NAME’, ‘sysname’) AS COLUMN_NAME,
x.value(‘@DATA_TYPE’, ‘sysname’) AS DATA_TYPE,
x.value(‘@IS_NULLABLE’, ‘VARCHAR(20)’) AS IS_NULLABLE,
x.value(‘@CHARLENGTH’, ‘VARCHAR(20)’) AS CHARLENGTH
INTO #tblXMLColumns
FROM @xmlTableSchema.nodes(‘/Table/C’) TempXML (x);

SET @strSQL = ‘ WITH Q AS (SELECT ‘;
SELECT @strSQL = @strSQL + ‘x.value(”@’ + COLUMN_NAME + ”’, ”’ + DATA_TYPE +
(CASE when CHARLENGTH is null then ” else ‘(‘ + CHARLENGTH + ‘)’ END) +
””+’) AS [‘ + COLUMN_NAME + ‘],’
FROM #tblXMLColumns;

SET @strSQL = LEFT(@strSQL, LEN(@strSQL) – 1);

SELECT @strSQL = @strSQL + ‘ FROM @xmlTableData.nodes(”/TableData/TableRow”) TempXML (x)) SELECT * FROM Q ‘
+ (CASE WHEN ISNULL(@strFilter,”) = ” THEN ” ELSE ‘ WHERE ‘ + @strFilter END);

EXEC sys.sp_executesql @strSQL,N’@xmlTableData xml’,@xmlTableData = @xmlTableData;

END TRY
BEGIN CATCH
RETURN -1;
END CATCH

LabelExit:
RETURN @intReturnValue;

END

————————————————————–

They can be used like this:

DECLARE@return_value int,
@xmlTableSchema xml,
@xmlTableData xml

EXEC@return_value = dbo.procConvertTableToXML
@strTable = N’NameOfTheTable’,
@strSchema = N’SchemaName’,
@xmlTableSchema = @xmlTableSchema OUTPUT,
@xmlTableData = @xmlTableData OUTPUT,
@strFilter = ‘Optional Filter like “ID < 100"'

SELECT@xmlTableSchema as N'@xmlTableSchema',
@xmlTableData as N'@xmlTableData'

SELECT'Return Value' = @return_value

EXEC@return_value = dbo.procConvertXMLToTable
@xmlTableSchema = @xmlTableSchema,
@xmlTableData = @xmlTableData,
@strFilter = 'Optional Filter like "ID < 100"'

GO

——————————————————–

Possible issue: If the amount of data is too big it returns only the schema.

Thanks for the idea,

Christian

]]>
Comment on Causes For Network Related Errors in SQL Server by Elisandro Lima Armindo https://sqlserverlearner.com/2012/01/10/causes-for-network-related-errors-in-sql-server/comment-page-1/#comment-452 Mon, 20 Oct 2014 20:59:54 +0000 https://sqlserverlearner.azurewebsites.net/2012/01/10/causes-for-network-related-errors-in-sql-server/#comment-452 muito obrigado, me ajudou muito thanks 🙂

]]>
Comment on Conversion failed when converting the varchar value xx to data type int. by sql1 https://sqlserverlearner.com/2012/05/16/conversion-failed-when-converting-the-varchar-value-xx-to-data-type-int/comment-page-1/#comment-1365 Mon, 08 Sep 2014 20:33:41 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/16/conversion-failed-when-converting-the-varchar-value-xx-to-data-type-int/#comment-1365 thanks for the article.. helpful!!

]]>
Comment on Concatenating Data From Different Rows into Single Column Row by Mad Computerist https://sqlserverlearner.com/2012/08/03/concatenating-data-from-different-rows-into-single-column-row/comment-page-1/#comment-2024 Wed, 16 Jul 2014 06:10:37 +0000 https://sqlserverlearner.azurewebsites.net/2012/08/03/concatenating-data-from-different-rows-into-single-column-row/#comment-2024 Thanks Abhijit,

Your code works perfectly.

]]>
Comment on sql server configuration manager remote procedure call failed by Nitin https://sqlserverlearner.com/2012/04/23/sql-server-configuration-manager-remote-procedure-call-failed/comment-page-1/#comment-814 Sat, 07 Dec 2013 13:56:17 +0000 https://sqlserverlearner.azurewebsites.net/2012/04/23/sql-server-configuration-manager-remote-procedure-call-failed/#comment-814 Thanks ,

Short & smart answer.

Thanks….

]]>
Comment on All about SQL Server Views by Frank F https://sqlserverlearner.com/2013/04/30/all-about-sql-server-views/comment-page-1/#comment-2167 Fri, 30 Aug 2013 14:09:26 +0000 https://sqlserverlearner.azurewebsites.net/2013/04/30/all-about-sql-server-views/#comment-2167 Wow! Finally found the solution to this issue – Thanks Devi, I’ve been looking everywhere for this.

]]>
Comment on How to call or execute a Stored Procedure from inside a Select Statement by Rahul https://sqlserverlearner.com/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/comment-page-1/#comment-1032 Tue, 16 Jul 2013 12:14:58 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/#comment-1032 SELECT * FROM OPENROWSET(‘SQLNCLI’
,’Server=(local);Trusted_Connection=Yes;Database=Master’
,’exec dbo.sp_getNewID A,13,@id)’)

Getting below error
Msg 15281, Level 16, State 1, Line 1
SQL Server blocked access to STATEMENT ‘OpenRowset/OpenDatasource’ of component ‘Ad Hoc Distributed Queries’ because this component is turned off as part of the security configuration for this server. A system administrator can enable the use of ‘Ad Hoc Distributed Queries’ by using sp_configure. For more information about enabling ‘Ad Hoc Distributed Queries’, search for ‘Ad Hoc Distributed Queries’ in SQL Server Books Online.

]]>
Comment on dbcc checktable repair_rebuild by Restaurando um Database Suspect – SQL Server | OnlyWhatMatters https://sqlserverlearner.com/2012/05/29/dbcc-checktable-repair_rebuild/comment-page-1/#comment-1751 Wed, 17 Apr 2013 10:01:14 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/29/dbcc-checktable-repair_rebuild/#comment-1751 […] É uma opção de reparo disĂłnĂ­vel somente em modo de emergĂȘncia – se vocĂȘ tentar usar o REPAIR_REBUILD, entĂŁo ele nĂŁo irĂĄ […]

]]>
Comment on sql server configuration manager remote procedure call failed by mazhar https://sqlserverlearner.com/2012/04/23/sql-server-configuration-manager-remote-procedure-call-failed/comment-page-1/#comment-813 Mon, 25 Mar 2013 04:35:51 +0000 https://sqlserverlearner.azurewebsites.net/2012/04/23/sql-server-configuration-manager-remote-procedure-call-failed/#comment-813 it solved my problem

]]>
Comment on How to call or execute a Stored Procedure from inside a Select Statement by Richard Parks https://sqlserverlearner.com/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/comment-page-1/#comment-1031 Sat, 16 Feb 2013 00:51:18 +0000 https://sqlserverlearner.azurewebsites.net/2012/05/09/how-to-call-or-execute-a-stored-procedure-from-inside-a-select-statement/#comment-1031 Appreciate your post.

This has some other good references regarding T-SQL EXEC statement:
http://msdn.microsoft.com/en-us/library/ms188332(SQL.105).aspx

]]>