Sunday, 6 December 2015

More than 8000 characters in Ms SQL Server handling with XML

Convert Delimited XML value in a column into Column (More than 8000 characters)

To handle more than 8000 characters, convert that data into XML format and insert into temp table.
Use this temp table wherever we want.

Step 1:
First convert your data into XML format.
If is comma separated varchar value, then go step 2.

Step 2:

For delimited value run below query.
select stuff((select distinct ','+cast(AddressID as varchar(max))
from Person.Address for xml path('') ),1,1,''))

Here my delimiter is ,(comma). I want to convert into XML.

Declare @Xml AS XML
SET @Xml =
( SELECT CAST (N'<r>' + REPLACE((select stuff((select distinct ','+cast(AddressID as varchar(max))
from Person.Address for xml path('') ),1,1,'')), ',', '</r><r>')  + '</r>' AS XML) AS [vals]  )
select @Xml

the selected XML value comes like below.
<r>29180</r>
<r>27611</r>
<r>22762</r>
<r>16843</r>
<r>17333</r>
<r>17665</r>
<r>24578</r>
<r>21802</r>
<r>26564</r>
<r>13205</r>
<r>499</r>

Convert that xml into table format.
SELECT A.value('.', 'varchar(max)') as AssessmentID FROM @Xml.nodes('r') AS FN(A) order by AssessmentID
The above query will give you table result. Insert insert this into temp table.

Below are the different ways to read the XML data in table format.
use [AdventureWorks2012]
go

DECLARE @XMLPath xml

set @XMLPath =(SELECT top 10 AddressID FROM Person.Address ORDER BY AddressID FOR XML PATH(''))

select @XMLPath
SELECT A.value('.', 'int') as AddressID FROM @XMLPath.nodes('AddressID') AS FN(A) order by AddressID


set @XMLPath =(SELECT top 10 AddressID FROM Person.Address ORDER BY AddressID FOR XML RAW('AddressIDs'))

SELECT A.value('@AddressID', 'int') as AddressID FROM @XMLPath.nodes('AddressIDs') AS FN(A) order by AddressID
select @XMLPath

Read one of the tag in XML format.

declare @DateXml xml =
'<Settings>
<NumOfRp>A</NumOfRp>
<InsID>ABCD</InsID>
<Terms>
<Term><Rpid>A</Rpid><MonthName>January</MonthName><Startdate>03/20/2015</Startdate><Enddate>03/24/2015</Enddate><Sortorder>1</Sorder></Term>
</Terms>
<YourComments>
 <CommentSettings>
     <AllPermitted>Y</AllPermitted><Predefined>Y</Predefined><Custom>Y</Custom><Personalized>N</Personalized><Century21st>Y</Century21st>
     <Unlimited>Y</Unlimited>
   </CommentSettings>
</YourComments>
</Settings>'

Now I want to take one of the tag CommentSettings as a XML data only.

SELECT
  Comments.query('.')
    AS Comments
FROM
  @DateXml.nodes('/Settings/Comments/RCCommentSettings')
    AS xmlData(Comments);

SELECT
  Comments.query('.')
    AS Comments
FROM
  @DateXml.nodes('/Settings/Comments')
    AS xmlData(Comments);

this XML data you may store in database.

Monday, 5 October 2015

Pass the variables to Another Procedure with OUTPUT Parameter

How to Pass the variables from one Procedure to Another Procedure with OUTPUT Parameter

Here is the Example:

Step 1: Create a Table 

CREATE TABLE  StudentsDetails

(
    [Studentid] [int] IDENTITY(1,1) NOT NULL,
    [Firstname] [nvarchar](200) NOT  NULL,
    [Lastname] [nvarchar](200)  NULL,
    [Email] [nvarchar](100)  NULL
)

Insert into StudentsDetails (Firstname, lastname, Email)
 Values('Vagalla', 'Suresh', 'vagalla.suresh@gmail.com')

Insert into StudentsDetails (Firstname, lastname, Email)
 Values('vagalla', 'Saran', 'vagalla.Saran@gmail.com')

Insert into StudentsDetails (Firstname, lastname, Email)
 Values('Rajesh', 'Kumar', 'Raj.Kumar@gmail.com')

Insert into StudentsDetails (Firstname, lastname, Email)
 Values('Prasad', 'Reddy', 'Prasad.reddy@gmail.com')

Insert into StudentsDetails (Firstname, lastname, Email)
 Values('Sukanya', 'reddy', 'Sukanya.reddy@gmail.com')

 select * from StudentsDetails

Step 2:
Declare the Parameters.
As of my concern, StudentID is the Input value and Expecting 3 output  values.

create  procedure GetStudentDetailsWithID
(

@studentid INT, --Input parameter ,  Studentid 
@studentFirstName VARCHAR (200) OUTPUT, -- Output parameter of student Firsst name
@studentLastName VARCHAR (200) OUTPUT, -- Output parameter of student Last name
@StudentEmail VARCHAR (200) OUTPUT -- Output Parameter of student email Address
)
AS
/*=====================================================================================================
With one input value, getting full details of the Student
=====================================================================================================*/
BEGIN
SELECT @studentFirstName = Firstname, 
@studentLastName = Lastname, 
@StudentEmail = email 
FROM StudentsDetails 
WHERE StudentID = @studentid
END

Step 3:
Now I am trying to get the Final result 

Create procedure ReturnFullData(@studentid int)
as
/*=====================================================================================================
With one input value, getting full details of the Student
=====================================================================================================*/
begin
Declare @studentFName as nvarchar(200)   -- Declare a variable to get studentFirstName
Declare @studentLName as nvarchar(50)     -- Declare a variable to get studentLastName
Declare @StudentEML as nvarchar(50)     -- Declare a variable to get Studentemail

Execute GetStudentDetailsWithID 1 , @studentFirstName = @studentFName output,  @studentLastName = @studentLName output, @StudentEmail = @StudentEML output

select @studentFName as FirstName,@studentLName as LastName ,@StudentEML   as Email   -- "Select" Statement is used to show the output from Procedure
end

-- here is your Fina output
exec ReturnFullData 1


Sunday, 28 June 2015

SQL Database Mail With Table Format

SQL Database Mail With Table Format


DECLARE 
@Body NVARCHAR(MAX),
@TableHead VARCHAR(1000),
@TableTail VARCHAR(1000)

SET @TableTail = '</table></body></html>' ;
SET @TableHead = '<html><head>' + '<style>'
    + 'td {border: solid black;border-width: 1px;padding-left:6px;padding-right:6px;padding-top:4px;padding-bottom:4px;font: 11px arial} '
    + '</style>' + '</head>' + '<body>' + 'Report generated on : '
    + CONVERT(VARCHAR(50), GETDATE(), 106) 
    + ' <br> <table cellpadding=0 cellspacing=0 border=0>' 
    + '<tr> <td bgcolor=#E6E6FA><b>Special Offer ID</b></td>'
    + '<td bgcolor=#E6E6FA><b>Description</b></td>'
    + '<td bgcolor=#E6E6FA><b>Type</b></td>'
    + '<td bgcolor=#E6E6FA><b>Category</b></td>'
    + '<td bgcolor=#E6E6FA><b>Start Date</b></td>'
    + '<td bgcolor=#E6E6FA><b>Today Date</b></td>'
    + '<td bgcolor=#E6E6FA><b>EndDate</b></td></tr>' ;

SET @Body = ( 
SELECT td = CAST([SpecialOfferID] AS VARCHAR(100)),'',
td = [Description],'',
td = [Type],'',
td = [Category] ,'',
td = CONVERT(VARCHAR(30),[StartDate],120) ,'',
td = CONVERT(VARCHAR(30),GETDATE(),120) ,'',
td = CONVERT(VARCHAR(30),[EndDate],120) 
FROM [AdventureWorks2012].[Sales].[SpecialOffer]
ORDER BY [SpecialOfferID]
FOR XML PATH('tr'),ELEMENTS )
SELECT  @Body = @TableHead + ISNULL(@Body, '') + @TableTail

EXEC msdb.dbo.sp_send_dbmail 
@profile_name = 'Database2012Mail',
@recipients='vagalla.suresh@gmail.com',
@subject = 'Work Order List',
@body = @Body,
@body_format = 'HTML' ;

Result:

EMP, DEPT Sample script

/****** Object:  Table [dbo].[DEPT]    Script Date: 19-05-2016 06:58:37 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET A...