SQL SERVER(T-SQL)

  • Hi All,

    I need to write a store procedure it should populate data as given below scenario

    Create the table called period_dim in the SQL Server database. Create a stored procedure to populate the table. Stored procedure should populate the table based on the year argument. Based on the year we pass u either insert or ignore the request. If that year has records, dont do any inserts.

    Can anyone help on this....

  • Declare @dates int

    set @dates=datepart(yyyy,'10/30/1988')

    print @dates

    if (select count(DATEPART(yyyy,years)) from period_dim where years=@dates)>0

    return

    else

    Insert into period_dim values(@dates)

    select * from period_dim

    i think u r asking this correct?

    Regards

    Guru

  • A bit faster:

    DECLARE @dates INT;

    SET @dates = DATEPART(yyyy,'10/30/1988');

    IF NOT EXISTS (SELECT 1 FROM dbo.period_dim WHERE years = @dates)

    BEGIN

    -- Complete SELECT statement

    INSERT INTO period_dim (col1, col2, ..., coln)

    SELECT ...

    FROM ... ;

    END

    Need an answer? No, you need a question
    My blog at https://sqlkover.com.
    MCSE Business Intelligence - Microsoft Data Platform MVP

  • Or:

    INSERT INTO period_dim (col1, col2, ..., coln)

    SELECT ...

    FROM ...

    WHERE NOT EXISTS

    (

    SELECT 1

    FROM dbo.period_dim WITH (HOLDLOCK)

    WHERE years = DATEPART(YEAR,'1988-10-30')

    );

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply