Good day,
I have a Time(DateTime) field in my SQL Server DB Table and I have to commit a record with a DateTime type on VS 2010 program to my DB. When I do I am getting the following error:
{"A column insert or update conflicts with a rule imposed by a previous CREATE RULE statement. The statement was terminated. The conflict occurred in database 'PTBODW', table 'dbo.MatchGPStoTripSchedule', column 'tTripScheduledStartTime'.\r\nThe statement has been terminated."} can anyone help me.
Thanks in advance.
Sounds like it may be a variable out of range. I'd fire up SQL profiler in SQL management studio and find the exact SQL that is causing the error. If its a bulk insert, I'd sucessively have the size of the insert until you isolat the (first?) row causing the issue.
Looks like the column is using the old sp_bindrule feature. You can list the rules in your database with:
select o.name as TableName
, c.name as ColumnName
, r.name as RuleName
from sys.objects o
join sys.columns c
on c.object_id = o.object_id
join sys.objects r
on r.object_id = c.rule_object_id
where o.is_ms_shipped <> 1
and rule_object_id <> 0
You can view the definition of a rule with sp_helptext:
exec sp_helptext 'RuleName'
Rules can be unbound from a column with sp_unbindrule (there can only be one rule per column, so sp_unbindrule does not need to know the rule's name):
exec sp_unbindrule 'MatchGPStoTripSchedule.tTripScheduledStartTime'
Related
In my C# program when I try to run one of my stored procedures, it gives me an exception which is related to a conflict between collations. How can I fix it?
Error Description: "Cannot resolve the collation conflict between
"Arabic_CI_AS" and "Latin1_General_CI_AS" in the equal to operation."
Collations in SQL Server define set of rules for comparing and sorting strings.
99 times out of 100 this particular error is a result of a cross-database query, with a join or where condition involving 'string' columns from tables in different databases (one of them typically being the tempdb).
You can specify a collation as part of a query, either a named one or the database_default keyword. See https://msdn.microsoft.com/en-us/library/ms184391.aspx for more info.
Just beware that this will practically disable (as far as this particular query is concerned) an index on the dynamically collated column if one exists.
I had the same error when creating sp that had some nvarchar arguments. These arguments were conditionally modified inside of this sp. Here's an example of the code that causes the error same as yours:
create procedure a
#name nvarchar(128),
as
if (#name = '')
throw 51000, 'Empty name', 1;
go
The solution here seems to be defining a collation (the best would be the database_default):
create procedure a
#name nvarchar(128),
as
if (#name = '' collate database_default)
throw 51000, 'Empty name', 1;
go
I am trying to insert null with my query into a datetime column which allows null. My query works fine (I think). But its putting 1900-01-01 00:00:00.000 instead of null into the datetime column. Why is this happening and how do I fix it ?
I created my own table to test this and nulling is NOT a problem there. But, it is a problem in another database. I think it must be something to do with the other database.
When inserting using INSERT query, don't specify the column name and don't give any value. That should insert null in the field.
For example, If Entry_Date is my nullable datetime column in abc table, then my insert statement would be like:
Insert into abc (Entry_Id, Entry_Value) values (1,1000);
By not mentioning the column, it should have null in it. Hope it helps.
To see all the triggers in the database, use the following query:
select
t.name as [TableName],
tr.name as [TriggerName],
m.[definition]
from sys.triggers tr
join sys.sql_modules m on m.object_id = tr.object_id
join sys.tables t on t.object_id = tr.parent_id
To see all the default constraints, use the following query:
select
t.name as [TableName],
c.name as [ColumnName],
dc.[name] as [ConstraintName],
dc.[definition]
from sys.tables t
join sys.columns c on c.object_id = t.object_id
join sys.objects do on do.object_id = c.default_object_id
join sys.default_constraints dc on dc.object_id= do.object_id
If your insert statement is O.K., then the only reason would be a trigger that alters the value of the insert. (Providing there isn't a bug on your SQL Server. :-) )
So check if your table you're inserting into does have triggers and what they do.
To see the list of triggers either select from sys.triggers in the DB where is the table or in SQL Server Management Studio in the Object Explorer go to the table and then expand it / Triggers - then you can check each trigger. You need to check INSTEAD OF triggers. But you might have a look also onto AFTER triggers if INSTEAD OF triggers don't cause this.
The other option is that the insert statement has a bug and the column defaults to 1900. In that case, are you sure you insert into the column you want ? Do you use INSERT Table(List of columns) Values and the order of columns and order of values is correct ?
My EF Remove fails with the above statement. The table (Product) has a single primary key (ProductID). Running SQL Trace produced the following SQL that causes the failure:
exec sp_executesql N'DELETE [dbo].[Product] WHERE ([ProductID] = #0)',N'#0 int',#0=620895
Full error statement:
Msg 121, Level 15, State 1, Procedure t_del_Product, Line 8 The select
list for the INSERT statement contains more items than the insert
list. The number of SELECT values must match the number of INSERT
columns.
Other similar Removes work just fine. My EDMX is fully updated against the DataSource (SQL Server 2012)
Any ideas? Anyone? Anyone?
Thanks!
UPDATE: I should have tried this earlier, but I get the same error even with a simple:
DELETE FROM Product Where ProductID = 620895
So, it is not EF.
As mentioned in my comment:
It looks like the sp is being called (possibly) when a delete occurs on dbo.Product and is producing your error. Just look on the table and see if there are triggers defined on it.
If indeed there is a trigger on the table calling this SP, then this is likely your issue and you should look into fixing the SP.
I have no experience with the .NET Entity Framework and I have some doubts about what exactly do this query:
using (MyCorpo.EarlyWarnings.Model.EarlyWarningsEntities context = new Model.EarlyWarningsEntities())
{
DBContext.SetTimeout(context);
model.VulnerabilitySeverityAverage = (from x in context.VulnerabilityAlertDocuments select x.Severity).Average();
}
(Where the type of the model.VulnerabilitySeverityAverage is simply a string)
So I think that VulnerabilityAlertDocuments map the VulnerabilityAlertDocument database table because into the EarlyWarningsEntities I have something this line:
public DbSet<VulnerabilityAlertDocument> VulnerabilityAlertDocuments { get; set; }
So I am executing a query on the VulnerabilityAlertDocuments DbSet object that represent a query on my VulnerabilityAlertDocument table on my database. Is it correct?
So what exatly do the previous query?
I think that it select the Severity field value of all records in the VulnerabilityAlertDocument table and calculate the avarage value from all these value.
Is it my reasoning correct?
How can I convert this entity query in a classic SQL query? Someone can help me?
Tnx
How can I convert this entity query in a classic SQL query?
To see actual SQL you can just call .ToString() method on your query;
var sql = (from x in context.VulnerabilityAlertDocuments select x.Severity).Average().ToString();
So I am executing a query on the VulnerabilityAlertDocuments DbSet
object that represent a query on my VulnerabilityAlertDocument table
on my database. Is it correct?
Yes
So what exatly do the previous query?
Your query will average value in Severity column of ValnerabilityAlertDocuments table.
your translated query would've looked simular to this:
SELECT
[GroupBy1].[A1] AS [C1]
FROM ( SELECT
AVG([Extent1].[Severity]) AS [A1]
FROM [dbo].[ValnerabilityAlertDocuments] AS [Extent1]
) AS [GroupBy1]
Also you could try to use such tool as SQL Server Profiler
UPDATE:
Just adding LinqPad to list of tools (thanks to Dismissile)
Select Average(x.Severity)
From VulnerabilityAlertDocuments x
Thats assuming your table is called "VulnerabilityAlertDocuments"
try again
used is c# sql vs 08 sql server 2005 express
whenever and where ever an sql select statement is used, its always like
select * from tablename
or count statement
is alsi like
select count something from table name
for selecting or doing anything on the tables,
i would like to know which tables exits in my the database i am connected to!
so like
select alltablenames from database_name.
please guide.
Personally, I would use the Information_Schema.Tables & Information_Schema.Columns views as these are views provided by Microsoft. (Rather than using the sysobjects tables)
to list all table of database
USE YourDBName
GO
SELECT *
FROM sys.Tables WHERE type='u'
to check table exists in database or not
IF EXISTS (SELECT 1
FROM sysobjects
WHERE xtype='u' AND name='tablename')
SELECT 'tablename exists.'
ELSE
SELECT 'tablename does not exist.'
The following query returns the names of the tables in an SQL Server database:
select name from sysobjects where xtype = 'U'
See http://database.ittoolbox.com/documents/finding-table-names-in-sql-18556 - simple queries for all table names and all columns for a given table.
or
EXEC sp_tables