INSERT INTO atable (x,y,z) VALUES ( NULL,NULL,NULL)
Answer from anon on Stack OverflowINSERT INTO atable (x,y,z) VALUES ( NULL,NULL,NULL)
If you're using SSMS (or old school Enterprise Manager) to edit the table directly, press CTRL+0 to add a null.
MSSQLServerDatabase ExecuteSQL insert NULL values
How to Enter NULL
c# - Insert null value into SQL Server database - Stack Overflow
Create Insert query with null value in SQL Server - Stack Overflow
If you were writing a tsql with literals, you would need to detect the null scenario and append "null" rather than the value, so the SQL looks like
1,2,null,4,5
If you see what I mean. However, this is a bad way to do it - your SQL right now is really really dangerous. You should parameterize instead - this solves a range of problems:
- sql injection(your code is a security risk right now)
- formatting (dates etc)
- null values
- query plan reuse (or the lack of it)
For example:
query = "insert into Assignment Values(@eventId, @branchId, @isPremiere, @isNew, ...)"
where you add a parameter with value for each of the placeholders. Note that due to how ado.net you need to represent nulls with DBNull:
cmd.Parameters.AddWithValue("foo",
foo == null ? (object)DBNull.Value : (object)foo);
(For each parameter)
Note that orms and micro-orms will help make this simple. For example with dapper:
DateTime foo = ...
int? bar = ...
connection.Execute(
@"insert ... values (@foo, @bar)",
new { foo, bar });
I have solved my problem below style
string sql= insert into Table(limit) values("+TBkapidaMinLimit.Text==""?"null":TBkapidaMinLimit.Text)+")"
If a value is NULL, then adding it to a string will produce a NULL. This allows us to add the quotes in the ISNULL check and just produce NULL in the true value of the check, producing the correct syntax for nulls or not nulls as necessary.
select 'Insert into Organizations(Name, IndustryId, ContactPerson, Email, Website, LocationId, ContactNumber, Mobilenumber) values(' +
IsNull(''''+Nameofthecompany+'''', 'NULL') + ', ' +
Isnull(''''+IndustryType+'''', 'NULL') + ', ' +
Isnull(''''+Nameofthepersonresponsibleforrecruitment+'''', 'NULL') + ', ' +
Isnull(''''+EmailId+'''', 'NULL') + ', ' +
Isnull(''''+websiteaddress+'''', 'NULL') + ', ' +
Isnull(''''+Location+'''', 'NULL') + ', ' +
Isnull(PhoneNumber, 'NULL') + ', ' +
Isnull(MobileNumber, 'NULL') + ')'
from Organization
If you want to use NULL (as a literal - not a string) for your NULL values, then the creation of the INSERT statement gets a lot more complicated; if the value is NULL, then you need to add the literal NULL without a leading and trailing '.
For each column where you want to do this, you'll need to use a CASE statement - something like this:
select 'INSERT INTO Organizations(.....) ' +
'VALUES(' +
CASE
WHEN NameOfTheCompany IS NOT NULL
THEN '''' + NameOfTheCompany + ''', '
ELSE 'NULL, '
END +
CASE
WHEN IndustryType IS NOT NULL
THEN '''' + IndustryType + ''', '
ELSE 'NULL, '
END +
..... and so on ......
+ ')'
... and so on, for each column you need this CASE statement ....