Search code examples
sqlsql-serverinsertnon-nullable

Insert statement with a NON-NULLABLE column


I have a SQL Server script that I'm using to insert some data into a database. I won't upload the whole script here just for space/time savings sake, but I will include the important bits.

So here is the problem. I have a table that has a column for some loginhtml, this column is of a non-nullable type. I would like for this column to be left blank on this particular add so it can default back to the parent that I'm pointing it at. So here we have the declaration for this important portion:

declare @loginTitle varchar(250), @loginHtml varchar(max)

And here we have what it will be set to:

set @loginHtml = null

And here is the insert part that is inevitably going to fail:

insert dbo.ApplicationLogin(ApplicationID, Title, Html)
   select @appID, @loginTitle, @loginHtml

EDIT: How can I have this script "default" the loginhtml column to whatever the PARENT Application is? Is there some "IF" statement/clause that can be used to accomplish this?


Solution

  • I may not have mentioned this in the initial post, and I would like to apologize for that. But the way the database is set up, is applications can have parent applications, so setting the loginhtml to the parent application and "technically" skipping adding it to the new application can be done by doing this:

    if(@loginHtml is not null)
    begin
        insert dbo.ApplicationLogin(ApplicationID, Title, Html)
        select @appID, @loginTitle, @loginHtml
    end
    

    This runs successfully and makes the "@loginhtml" default to whatever the parent application has set for that value.