SqlServer simple operation of XML and SQl try catch of unified format

1: SqlServer simple operation XML:

 

. The ALTER PROCEDURE [the dbo] [SP_CRM_FranchiseeRecharge_Money] 
    @Create_By VARCHAR ( 50 ), 
    @ xmlStr1 NVARCHAR (MAX)   - written in the inside of the parameters passed to 
the AS 
the BEGIN   AS variable custom begin later in 
    the SET the NOCOUNT the ON; 
    the DECLARE @ XML xml = @ xmlStr1; 

sqlserver operating XML 

     T - SQL provides several types of queries against XML function: 
      query (XQuery): Xquery query using the qualifying elements of the collection. 
      value (XQuery, Type): using the specific element Xquery query value, a defined value. Type. 
      exist (XQuery): use Xquery query whether the conditions of the elements are present. 
      nodes (XQuery): Xquery obtained using a result set. 



INSERT INTO qqtest (xmlinfo) xmlinfo type string 
the VALUES ( 
N ' <the root>
<person>
<id>F001</id>
<name>F001</name>
<age>18</age>
</person>
</root>'
)


DECLARE @errorCode INT;
DECLARE @errorMsg VARCHAR(2000);
BEGIN TRY
    SET @errorCode = 0;
    SET @errorMsg = '';
    DECLARE @xml XML;
    SELECT @xml = xmlinfo
    FROM dbo.qqtest
    WHERE id =6;

    SELECT S.value('id[1]', 'varchar(30)') id,
           S.value ( ' name [. 1] ' , ' VARCHAR (30) ' ) _Name, 
           s.value ( ' Age [. 1] ' , ' VARCHAR (30) ' ) Age 
    the FROM @ xml.nodes ( ' / the root / Person ' ) the aS a (S); // B (S) are no problem @ xml.nodes (' / root / person ') aS a (S) as a table to operated 
    the SET @errorCode = 200 is ; 
the END TRY 
the BEGIN the CATCH 
    the SET @errorCode = 500 ; 
    the SET @errorMsg = ERROR_MESSAGE (); 
    the RAISERROR (N 'error: XML file format error: ' , 16 , . 1 , @ errorMsg); 
    the PRINT (@errorMsg) 
the END the CATCH 



acquired XML data, XML data as a table xml.nodes @ ( ' / Root / Data ' ) the AS T (S ); 

SELECT s.value ( ' ID [. 1] ' , ' int ' ) AS ID, s.value ( ' the Name [. 1] ' , ' nvarchar (50) ' ) AS the Name 
 the FROM @ xml.nodes ( ' / Root / Data ' ) the AS T (S); 

     2  written to the temporary tables
        the INSERT the INTO #tbCRM_FranchiseeRecharge
        (
            [Recharge_ID],
            [Franchisee_ID],
            [Recharge_Credit],
            [Recharge_Amount]
        )
        SELECT DISTINCT
               NEWID(),
               S.value('Franchisee_ID[1]', 'uniqueidentifier') AS Franchisee_ID,
               S.value('Recharge_Credit[1]', 'int') AS Recharge_Credit,
               S.value('Recharge_Amount[1]', 'money') AS Recharge_Amount
        FROM @xml.nodes('/Root/data') AS T(S);

 

2: SQl statement simple uniform format try catch: the following simple Sql

  string sql = @"BEGIN
                                SET @insertStateCode = 0;
                                IF NOT EXISTS
                                (
                                    SELECT TOP 1
                                           *
                                    FROM tbCRM_Franchisee_Credit_History
                                    WHERE Seq =@rechangeID
                                )          
                                BEGIN TRY
                                    BEGIN TRAN mytran; 
                                    INSERT INTO tbCRM_Franchisee_Credit_History
                                    (
                                        [Seq],
                                        [Franchisee_ID],
                                        [Tran_Code],
                                        [Tran_By],
                                        [Tran_Date],
                                        [Credit],
                                        [Amount],
                                        [Remark]
                                    )
                                    SELECT RM.Recharge_ID,
                                           RM.Franchisee_ID,
                                           5,
                                           RM.Create_By,
                                           GETDATE(),
                                           RM.Recharge_Credit,
                                           RM.Recharge_Amount,
                                           RM.Remark
                                    FROM tbCRM_Franchisee_Recharge_Master RM
                                    WHERE RM.Recharge_ID = @rechangeID;                            
                                    UPDATE tbCRM_Franchisee_Recharge_Master
                                    SET Approve_By = 1
                                    WHERE Recharge_ID =@rechangeID;
                                    SELECT @insertStateCode =200;
                                    TRAN Mytran a COMMIT; 
                                END TRY
                                BEGIN CATCH
                                    the SELECT @insertStateCode = 500; 
                                    ROLLBACK TRAN Mytran; 
                                the END the CATCH; 
                            the END; " ; 

actually simple as follows:
BEGIN TRY
    BEGIN TRAN mytran; 
END TRY
BEGIN CATCH
    SELECT @insertStateCode = 500;
      ROLLBACK TRAN mytran;
END CATCH;


 

Guess you like

Origin www.cnblogs.com/Fengge518/p/11802598.html