Insert default value when parameter is null

后端 未结 16 1972
遥遥无期
遥遥无期 2020-12-24 05:27

I have a table that has a column with a default value:

create table t (
    value varchar(50) default (\'something\')
)

I\'m using a stored

相关标签:
16条回答
  • 2020-12-24 05:43

    You can use default values for the parameters of stored procedures:

    CREATE PROCEDURE MyTestProcedure ( @MyParam1 INT,
    @MyParam2 VARCHAR(20) = ‘ABC’,
    @MyParam3 INT = NULL)
    AS
    BEGIN
        -- Procedure body here
    
    END
    

    If @MyParam2 is not supplied, it will have the 'ABC' value...

    0 讨论(0)
  • 2020-12-24 05:43

    The pattern I generally use is to create the row without the columns that have default constraints, then update the columns to replace the default values with supplied values (if not null).

    Assuming col1 is the primary key and col4 and col5 have a default contraint

    -- create initial row with default values
    insert table1 (col1, col2, col3)
        values (@col1, @col2, @col3)
    
    -- update default values, if supplied
    update table1
        set col4 = isnull(@col4, col4),
            col5 = isnull(@col5, col5)
        where col1 = @col1
    

    If you want the actual values defaulted into the table ...

    -- create initial row with default values
    insert table1 (col1, col2, col3)
        values (@col1, @col2, @col3)
    
    -- create a container to hold the values actually inserted into the table
    declare @inserted table (col4 datetime, col5 varchar(50))
    
    -- update default values, if supplied
    update table1
        set col4 = isnull(@col4, col4),
            col5 = isnull(@col5, col5)
        output inserted.col4, inserted.col5 into @inserted (col4, col5)
        where col1 = @col1
    
    -- get the values defaulted into the table (optional)
    select @col4 = col4, @col5 = col5 from @inserted
    

    Cheers...

    0 讨论(0)
  • 2020-12-24 05:45

    With enough defaults on a table, you can simply say:

    INSERT t DEFAULT VALUES
    

    Note that this is quite an unlikely case, however.

    I've only had to use it once in a production environment. We had two closely related tables, and needed to guarantee that neither table had the same UniqueID, so we had a separate table which just had an identity column, and the best way to insert into it was with the syntax above.

    0 讨论(0)
  • 2020-12-24 05:49

    This is the best I can come up with. It prevents sql injection uses only one insert statement and can ge extended with more case statements.

    CREATE PROCEDURE t_insert (     @value varchar(50) = null )
    as
    DECLARE @sQuery NVARCHAR (MAX);
    SET @sQuery = N'
    insert into __t (value) values ( '+
    CASE WHEN @value IS NULL THEN ' default ' ELSE ' @value ' END +' );';
    
    EXEC sp_executesql 
    @stmt = @sQuery, 
    @params = N'@value varchar(50)',
    @value = @value;
    
    GO
    
    0 讨论(0)
  • 2020-12-24 05:49

    Hope To help to -newbie as i am- Ones who uses Upsert statements in MSSQL.. (This code i used in my project on MSSQL 2008 R2 and works simply perfect..May be It's not Best Practise.. Execution time statistics shows execution time as 15 milliSeconds with insert statement)

    Just set your column's "Default value or binding" field as what you decide to use as default value for your column and Also set the column as Not accept null values from design menu and create this stored Proc..

    `USE [YourTable]
    GO
    
    
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE PROC [dbo].[YourTableName]
    
        @Value smallint,
        @Value1 bigint,
        @Value2 varchar(50),
        @Value3 varchar(20),
        @Value4 varchar(20),
        @Value5 date,
        @Value6 varchar(50),
        @Value7 tinyint,
        @Value8 tinyint,
        @Value9 varchar(20),
        @Value10 varchar(20),
        @Value11 varchar(250),
        @Value12 tinyint,
        @Value13 varbinary(max) 
    

    -- in my project @Value13 is a photo column which storing as byte array.. --And i planned to use a default photo when there is no photo passed --to sp to store in db..

    AS
    --SET NOCOUNT ON
    IF @Value = 0 BEGIN
        INSERT INTO YourTableName (
            [TableColumn1],
            [TableColumn2],
            [TableColumn3],
            [TableColumn4],
            [TableColumn5],
            [TableColumn6],
            [TableColumn7],
            [TableColumn8],
            [TableColumn9],
            [TableColumn10],
            [TableColumn11],
            [TableColumn12],
            [TableColumn13]
        )
        VALUES (
            @Value1,
            @Value2,
            @Value3,
            @Value4,
            @Value5,
            @Value6,
            @Value7,
            @Value8,
            @Value9,
            @Value10,
            @Value11,
            @Value12,
            default
        )
        SELECT SCOPE_IDENTITY() As InsertedID
    END
    ELSE BEGIN
        UPDATE YourTableName SET 
            [TableColumn1] = @Value1,
            [TableColumn2] = @Value2,
            [TableColumn3] = @Value3,
            [TableColumn4] = @Value4,
            [TableColumn5] = @Value5,
            [TableColumn6] = @Value6,
            [TableColumn7] = @Value7,
            [TableColumn8] = @Value8,
            [TableColumn9] = @Value9,
            [TableColumn10] = @Value10,
            [TableColumn11] = @Value11,
            [TableColumn12] = @Value12,
            [TableColumn13] = @Value13
        WHERE [TableColumn] = @Value
    
    END
    GO`
    
    0 讨论(0)
  • 2020-12-24 05:54

    chrisofspades,

    As far as I know that behavior is not compatible with the way the db engine works, but there is a simple (i don't know if elegant, but performant) solution to achive your two objectives of DO NOT

    1. maintain the default value in multiple places, and
    2. use multiple insert statements.

    The solution is to use two fields, one nullable for insert, and other one calculated to selections:

    CREATE TABLE t (
        insValue VARCHAR(50) NULL
        , selValue AS ISNULL(insValue, 'something')
    )
    
    DECLARE @d VARCHAR(10)
    INSERT INTO t (insValue) VALUES (@d) -- null
    SELECT selValue FROM t
    

    This method even let You centralize the management of business defaults in a parameter table, placing an ad hoc function to do this, vg changing:

    selValue AS ISNULL(insValue, 'something')
    

    for

    selValue AS ISNULL(insValue, **getDef(t,1)**)
    

    I hope this helps.

    0 讨论(0)
提交回复
热议问题