---------------------------------------------------------------------
-- SQL Server 2005XgAhvV[WvO~O
-- 16 fobO
---------------------------------------------------------------------

---------------------------------------------------------------------
-- 16.4 CLR f[^x[XIuWFNg̃fobO
---------------------------------------------------------------------

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void cp_DebugTest()
    {
        int count = 1;
        SqlContext.Pipe.Send("Hello world 1!\n");
        count++;
        SqlContext.Pipe.Send("Hello world 2!\n");

        using (SqlConnection conn =
            new SqlConnection("Context Connection=true"))
        {
            string EqType = "";

            // XgAhvV[W̎sɎgpR}hIuWFNgݒ
            SqlCommand cmd = new SqlCommand("dbo.ap_EqType_List", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // XgAhvV[Ws
            conn.Open();

            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read()) // ̍sȂȂ܂ōsi߂
                {
                    // f[^Xg[߂ꂽo̓p[^Ԃ
                    //id = reader.GetInt32(0); // ŏ̗͖
                    EqType = EqType + reader.GetString(1) + ", ";
                }
            }
        }
    }
};


EXEC dbo.cp_DebugTest

---------------------------------------------------------------------
-- 16.5 fobK̑p
---------------------------------------------------------------------

@debug INT = 0


IF @debug <> 0
     SELECT @chvProperty Property,
            @chvValue Value,
            @chvUnit Unit

IF @debug <> 0
    SELECT * FROM #Properties


ALTER PROCEDURE ap_InventoryProperties_Get
/************************************************************
ĩvpeBÂ悤ɃZ~Rŋ؂XgԂ
Property=Value Unit; Property=Value Unit; Property=Value Unit; ...

eXg@F
DECLARE @p VARCHAR(max)
EXEC ap_InventoryProperties_Get 5, @p OUTPUT, 1
SELECT @p
*************************************************************/
(
    @intInventoryId INT,
    @chvProperties VARCHAR(max) OUTPUT,
    @debug INT = 0
)
AS
    DECLARE @intCountProperties INT,
            @intCounter INT,
            @chvProperty VARCHAR(50),
            @chvValue VARCHAR(50),
            @chvUnit VARCHAR(50),
            @chvProcedure SYSNAME

    SET @chvProcedure = 'ap_InventoryProperties_Get'

    IF @debug <> 0
        SELECT '**** ' + @chvProcedure + 'START ****'

    CREATE TABLE #Properties(
        Id INT IDENTITY(1, 1),
        Property VARCHAR(50),
        Value VARCHAR(50),
        Unit VARCHAR(50)
    )

    -- ĩvpeB肷
    INSERT INTO #Properties (Property, Value, Unit)
        SELECT Property, Value, Unit
        FROM dbo.InventoryProperty InventoryProperty
        INNER JOIN dbo.Property Property
            ON InventoryProperty.PropertyId = Property.PropertyId
        WHERE InventoryProperty.InventoryId = @intInventoryId

    IF @debug = 1
        SELECT * FROM #Properties

    -- [vݒ肷
    SELECT @intCountProperties = COUNT(*),
           @intCounter = 1,
           @chvProperties = ''
    FROM #Properties

    -- vpeBXgŜ[v
    WHILE @intCounter <= @intCountProperties
    BEGIN
        -- evpeB̒l擾
        SELECT @chvProperty = Property,
               @chvValue = Value,
               @chvUnit = Unit
        FROM #Properties
        WHERE Id = @intCounter

        IF @debug <> 0
            SELECT @chvProperty Property,
                   @chvValue Value,
                   @chvUnit Unit

        -- Xg쐬
        SET @chvProperties = @chvProperties + '; '
                           + @chvProperty + '='
                           + @chvValue + ' ' + ISNULL(@chvUnit, '')

        IF @debug = 1
            SELECT @chvProperties [@chvProperties],
                   @intCounter [@intCounter]

        -- Oɖ߂Ď̔i擾
        SET @intCounter = @intCounter + 1
    END

    IF SUBSTRING(@chvProperties, 0, 2) = '; '
        SET @chvProperties = RIGHT(@chvProperties, LEN(@chvProperties) - 2)

    DROP TABLE #Properties

    IF @debug <> 0
        SELECT '**** ' + @chvProcedure + 'END ****'

    RETURN 0


DECLARE @chvResult VARCHAR(max)
EXEC dbo.ap_InventoryProperties_Get
    @intInventoryId = 5,
    @chvProperties = @chvResult OUTPUT,
    @debug = 1

SELECT @chvResult Result


EXEC dbo.ap_InventoryProperties_Get
          @intInventoryId = 5,
          @chvProperties = @chvResult OUTPUT


DECLARE @chvProcedure SYSNAME
SET @chvProcedure = 'ap_InventoryProperties_Get'

IF @debug <> 0
    SELECT '**** '+ @chvProcedure + 'START ****'
...

IF @debug <> 0
    SELECT '**** '+ @chvProcedure + 'END ****'

RETURN 0


EXEC dbo.ap_InventoryProperties_Get
    @intInventoryId,
    @chvProperties OUTPUT,
    @debug


UPDATE dbo.EquipmentBC
SET EqBC = '111'
    OUTPUT deleted.*
WHERE EqID = 1


INSERT dbo.Equipment2(EqTypeID, Make, Model)
    OUTPUT inserted.EqId, inserted.EqTypeID,
           inserted.Make + ' - ' + inserted.Model
VALUES (22, 'Alfa', '2000X');


DECLARE @tbl TABLE(EqId INT,
                   EqTypeID INT,
                   Eq VARCHAR(100));

DELETE dbo.Equipment2
    OUTPUT deleted.EqId,
           deleted.EqTypeID,
           deleted.Make + ' - ' + deleted.Model
    INTO @tbl
WHERE Make LIKE 'A%';

SELECT t.*, EqType.EqType
FROM @tbl t INNER JOIN EqType
ON t.EqTypeID = EqType.EqTypeID

---------------------------------------------------------------------
-- 16.7 T^IȃG[
---------------------------------------------------------------------

DECLARE @intRowCount INT
DECLARE @intErrorCode INT

UPDATE Inventory
SET StatusId = 3
WHERE InventoryID = -11

SELECT @intRowCount = @@ROWCOUNT,
       @intErrorCode = @@ERROR

IF @intRowCount = 0
    SELECT N'R[hXV܂ł!'


CONVERT(VARCHAR, @intPropertyId)


DECLARE @test VARCHAR
SET @test = '123456789012345678901234567890'
SELECT DATALENGTH(@test), @test


/************************************************************
**     SELECT *
**     FROM #Properties
*************************************************************/


CREATE PROCEDURE dbo.ap_DeferredNameResolution
AS
    SET NOCOUNT ON
    SELECT 'Start'
    SELECT * FROM NonExistingTable
    SELECT 'Will execution be stopped?'
    RETURN

