Manoz
Manoz

Reputation: 6587

SQL Server: Insert if column values are null

I am newbie to SQL queries so sorry for this basic question.

I want to insert values into a table where column's value is null

I tried following

INSERT INTO SystemUsers(FilePath)
If @FilePath IS Null
values('C:\Users\Developer\Desktop\MvcApplication8\MvcApplication8\App_Data\Uploads\Lighthouse.jpg')

and

INSERT INTO SystemUsers(FilePath)
where FilePath IS Null
values('C:\Users\Developer\Desktop\MvcApplication8\MvcApplication8\App_Data\Uploads\Lighthouse.jpg')

But that didn't work, how can I insert default values in a column whether column's values are null?

Upvotes: 0

Views: 1021

Answers (2)

M.Ali
M.Ali

Reputation: 69524

UPDATE SystemUsers
    SET FilePath = 'C:\Users\Developer\Desktop\MvcApplication8\MvcApplication8\App_Data\Uploads\Lighthouse.jpg'
WHERE FilePath is null

Where clause normaly comes after the operation you are doing.

Upvotes: 0

jjchiw
jjchiw

Reputation: 4445

Maybe what you want is the UPDATE command and not the insert something like this

UPDATE SystemUsers
SET FilePath = 'C:\Users\Developer\Desktop\MvcApplication8\MvcApplication8\App_Data\Uploads\Lighthouse.jpg'
WHERE FilePath is null

Upvotes: 9

Related Questions