简体   繁体   中英

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?

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
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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM