How can I set the value of a SQL Server column to True / False? - sql-server

I have this SQL:
var sql = #"UPDATE TestQuestion
SET UserDataJSON = #UserDataJSON,
Answered = TRUE
WHERE TestQuestionId = #TestQuestionId";
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter("#TestQuestionId", testQuestionId));
CREATE TABLE [dbo].[TestQuestion] (
[TestQuestionId] INT IDENTITY (1, 1) NOT NULL,
[TestId] VARCHAR (20) NOT NULL,
[Answered] BIT DEFAULT ((0)) NULL,
CONSTRAINT [PK_TestQuestion] PRIMARY KEY CLUSTERED ([TestQuestionId] ASC)
);
How can I set the Answered to TRUE? I tried this but it mentions about invalid
column name.

SQL Server, technically, has neither a boolean data type nor any boolean literals. It has the bit data type, that you've indicated that you're using, which is a numeric type accepting the values 0 or 1. Some data access layers (such as ADO) will pretend that this is a boolean type in foreign languages (such as C#), but such data access layers aren't involved when you're writing a piece of literal SQL code as a string
If you wish to continue to pretend that SQL Server has a boolean data type, because ADO pretends that it does, then you need to let ADO perform the necessary adjustments for you:
var sql = #"UPDATE TestQuestion
SET UserDataJSON = #UserDataJSON,
Answered = #Answered
WHERE TestQuestionId = #TestQuestionId";
List<SqlParameter> parameterList = new List<SqlParameter>();
parameterList.Add(new SqlParameter("#TestQuestionId", testQuestionId));
parameterList.Add(new SqlParameter("#Answered", true));
ADO will translate a boolean value in a parameter into a value suitable for a SQL Server bit type - 0 or 1.

Try this: (Give the value within ' i.e. 'TRUE')
var sql = #"UPDATE TestQuestion
SET UserDataJSON = #UserDataJSON,
Answered = 'TRUE'
WHERE TestQuestionId = #TestQuestionId";
Otherwise, it will be taken as Column Name. So set the value with '

Replace TRUE with 1: ie, you want to set Answered = 1 because this is a bit type. 1 represents TRUE in TSQL.

Related

DbString, IsFixedLength and IsAnsi for varchar

I am new to Dapper, want to know why below is suggested, when my code runs without it?
Ansi Strings and varchar
Dapper supports varchar params, if you are executing a where clause on
a varchar column using a param be sure to pass it in this way:
Query<Thing>("select * from Thing where Name = #Name", new {Name = new
DbString { Value = "abcde", IsFixedLength = true, Length = 10, IsAnsi
= true });
On SQL Server it is crucial to use the unicode when querying unicode
and ansi when querying non unicode.
Below is my code, which runs against SQL server 2012 without using DbString etc.
create table Author (
Id int identity(1,1),
FirstName varchar(50),
LastName varchar(50)
);
go
insert into Author (FirstName, LastName) values ('Tom', 'John');
public Author FindByVarchar(string firstName)
{
using (IDbConnection db = DBHelper.NewSqlConnection())
{
return db.Query<Author>("Select * From Author WHERE FirstName = #firstName", new { firstName }).SingleOrDefault();
}
}
Questions:
1 Why is DbString type used in this case?
2 Why the length is set to 10 (e.g.Length = 10) when "abcde" is 5?
3 Do I still need to use DbString when my current code works?
4 Is it correct to set IsAnsi = false for unicode column?
5 For varchar column, is it correct to set IsFixedLength = false, and ignore setting Length?
The purpose of the example is that it is describing a scenario where the data type is char(10). If we just used "abcde", Dapper might think that nvarchar(5) was appropriate. This would be very inefficient in some cases - especially in a where clause, since the RDBMS can decide that it can't use the index, and instead needs to table scan doing a string conversion for every row in the table from char(10) to the nvarchar version. It is for this reason that DbString exists - to help you control exactly how Dapper configures the parameter for text data.
I think this answers your 1 and 2.
3: are you using ANSI (non-unicode text) or fixed-width text? Note that the ANSI default can also be set globally if you always avoid unicode
4: yes
5: yes
4+5 combined: if you're using nvarchar: just use string

How can I use a SQL Server column of type rowversion?

With Visual Basic in Visual Studio I am trying to select a row of type rowversion from a SQL Server database.
'version' is a column of type rowversion
SELECT [Version] FROM Employees WHERE Employee_id = 1
Then in VB - To get value from version column
Dim myBuffer As Byte()
Dim reader As SqlDataReader
numRead = reader.GetBytes(0, 0, myBuffer, 0, 16)
I know there is data in mybuffer from this:
For i = 0 To myBuffer.Length - 1
MsgBox(myBuffer(i).ToString())
Next
Also myBuffer.Length = 9
But when I want to query my database with myBuffer...
cmd.Parameters.AddWithValue("version", myBuffer)
I get the error:
Procedure or function 'updatePerson' expects parameter '#version', which was not supplied.
As if myBuffer in NULL.
Does anybody know to get the rowversion out of a database and then use it in a query?

Update table column using table in another SQL Server database while using a spatial function

I would like to update a table column using a spatial function with another database table. This is what I've come up with....
UPDATE FirstDatabase.dbo.track_logs
SET county = t2.CountyName
FROM OtherDatabase.dbo.tblCounty AS t2
WHERE t2.cty_geog.STIntersects(
GEOGRAPHY::STPointFromText('Point(' + FirstDatabase.dbo.track_logs.lng + ' ' +
FirstDatabase.dbo.track_logs.lat + ')', 26915)
)
but I get this error...
An expression of non-boolean type specified in a context where a condition is expected, near ')'.
Almost there. This is one of those silly things in TSQL. You need a = 1.
That's the answer to your question, but I would also be tempted to use POINT(lat, lon, SRID) to address your comment.
UPDATE FirstDatabase.dbo.track_logs
SET county = t2.CountyName
FROM OtherDatabase.dbo.tblCounty AS t2
WHERE t2.cty_geog.STIntersects(geography::Point(FirstDatabase.dbo.track_logs.lat,
FirstDatabase.dbo.track_logs.lng,
26915)) = 1
In a language like C#, you could write something like:
bool val = true;
if (val)
// do stuff
But in TSQL, you have to write the equivalent to:
bool val = true;
if (val == true)
// do stuff
This isn't specific to SQL Spatial, of course, you'd also have to specify WHERE bitColumnName = 1 or, as your example illustrates, WHERE bitReturningFunction(args) = 1.

Cannot understand how will Entity Framewrok generate a SQL statement for an Update operation using timestamp?

I have the following method inside my asp.net mvc web application :
var rack = IT.ITRacks.Where(a => !a.Technology.IsDeleted && a.Technology.IsCompleted);
foreach (var r in rack)
{
long? it360id = technology[r.ITRackID];
if (it360resource.ContainsKey(it360id.Value))
{
long? CurrentIT360siteid = it360resource[it360id.Value];
if (CurrentIT360siteid != r.IT360SiteID)
{
r.IT360SiteID = CurrentIT360siteid.Value;
IT.Entry(r).State = EntityState.Modified;
count = count + 1;
}
}
IT.SaveChanges();
}
When I checked SQL Server profiler I noted that EF will generated the following SQL statement:
exec sp_executesql N'update [dbo].[ITSwitches]
set [ModelID] = #0, [Spec] = null, [RackID] = #1, [ConsoleServerID] = null, [Description] = null, [IT360SiteID] = #2, [ConsoleServerPort] = null
where (([SwitchID] = #3) and ([timestamp] = #4))
select [timestamp]
from [dbo].[ITSwitches]
where ##ROWCOUNT > 0 and [SwitchID] = #3',N'#0 int,#1 int,#2 bigint,#3 int,#4 binary(8)',#0=1,#1=539,#2=1502,#3=1484,#4=0x00000000000EDCB2
I can not understand the purpose of having the following section :-
select [timestamp]
from [dbo].[ITSwitches]
where ##ROWCOUNT > 0 and [SwitchID] = #3',N'#0 int,#1 int,#2 bigint,#3 int,#4 binary(8)',#0=1,#1=539,#2=1502,#3=1484,#4=0x00000000000EDCB2
Can anyone advice?
Entity Framework uses timestamps to check whether a row has changed. If the row has changed since the last time EF retrieved it, then it knows it has a concurrency problem.
Here's an explanation:
http://www.remondo.net/entity-framework-concurrency-checking-with-timestamp/
This is because EF (and you) want to update the updated client-side object by the newly generated rowversion value.
First the update is executed. If this succeeds (because the rowversion is still the one you had in the client) a new rowversion is generated by the database and EF retrieves that value. Suppose you'd immediately want to make a second update. That would be impossible if you didn't have the new rowversion.
This happens with all properties that are marked as identity or computed (by DatabaseGenertedOption).

how to overwrite repeat data in the database in a efficient way?

I use Sql server 2008 to store my data,and the table structure like that
index float not null,
type int not null,
value int not null,
and the (index,type) is unique.there are not two datas has the same index and the same type.
So when I insert the data to the table, I have to check the (index,type) pair whether in the table already, if it exists I use update statement, otherwise, I insert it directly.but I think this is not a efficient way,because:
Most of the data' index-type pair is not existed int the table.so the select operation is waste, especially the table is huge.
When I use C# or other CLR language to insert the data, I can't use batch copy or batch insert.
is there any way to overwrite the data directly without check whether it is existed in the table?
If you want to update OR insert the data, you need to use merge:
merge MyTable t using (select #index index, #type type, #value value) s on
t.index = s.index
and t.type = s.type
when not matched insert (index, type value) values (s.index, s.type, s.value)
when matched update set value = s.value;
This will look at your values and take the appropriate action.
To do this in C#, you have to use the traditional SqlClient:
SqlConnection conn = new SqlConnection("Data Source=dbserver;Initial Catalog=dbname;Integrated Security=SSPI;");
SqlCommand comm = new SqlCommand();
conn.Open();
comm.Connection = conn;
//Add in your values here
comm.Parameters.AddWithValue("#index", index);
comm.Parameters.AddWithValue("#type", type);
comm.Parameters.AddWithValue("#value", value);
comm.CommandText =
"merge MyTable t using (select #index index, #type type, #value value) s on " +
"t.index = s.index and t.type = s.type " +
"when not matched insert (index, type value) values (s.index, s.type, s.value) " +
"when matched update set value = s.value;"
comm.ExecuteNonQuery();
comm.Dispose();
conn.Close();
conn.Dispose();
You should make (index, type) into a composite primary key (aka compound key).
This would ensure that the table can only even have unique pairs of these (I am assuming the table does not have a primary key already).
If the table does have a primary key, you can add a UNIQUE constraint onto those columns with similar effect.
Once defined, this means that any attempt to insert a duplicate pair would fail.
Other answers recommend constraints. Creating constraints just means you will be executing insert statements that trigger errors. The next step (after having created the constraints) is something like INSERT ON DUPLICATE KEY UPDATE, which apparently does have an Sql Server equivalent.

Resources