sql-server-2005

Entity Framework - default values doesn't set in sql server table

风格不统一 提交于 2019-12-17 07:49:46
问题 SQL server 2005 database table has a column 'createdon' for which default value set to getdate(). I am trying to add a record using entity framework. 'createdon' column is not getting updated. Did I miss any property in Entity framework, please suggest. 回答1: This is one of the few issues that are problematic with Entity Framework. Say you have a class that looks like this: public class MyEntity { // Id is a PK on the table with Auto-Increment public int Id { get; set; } // CreatedOn is a

sql use statement with variable

点点圈 提交于 2019-12-17 07:31:17
问题 I'm trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed: USE @DatabaseName EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']' To add a little more detail. EDIT: I would like to perform several things on two separate database, where both are configured with a variable. Something like this: USE Database1 SELECT * FROM Table1 USE Database2 SELECT * FROM Table2 回答1: exec sp_execsql @Sql The DB change only lasts for the

sql use statement with variable

删除回忆录丶 提交于 2019-12-17 07:31:11
问题 I'm trying to switch the current database with a SQL statement. I have tried the following, but all attempts failed: USE @DatabaseName EXEC sp_sqlexec @Sql -- where @Sql = 'USE [' + @DatabaseName + ']' To add a little more detail. EDIT: I would like to perform several things on two separate database, where both are configured with a variable. Something like this: USE Database1 SELECT * FROM Table1 USE Database2 SELECT * FROM Table2 回答1: exec sp_execsql @Sql The DB change only lasts for the

When to use EXCEPT as opposed to NOT EXISTS in Transact SQL?

半世苍凉 提交于 2019-12-17 07:23:19
问题 I just recently learned of the existence of the new "EXCEPT" clause in SQL Server (a bit late, I know...) thru reading code written by a coworker. It truly amazed me! But then I have some questions regarding its usage: when is it recommended to be employed? Is there a difference, performance-wise, between using it versus a correlated query employing "AND NOT EXISTS..."? After reading EXCEPT's article in the BOL I thought it was just a shorthand for the second option, but was surprised when I

Check if a string contains a substring in SQL Server 2005, using a stored procedure

北城以北 提交于 2019-12-17 07:14:59
问题 I've a string, @mainString = 'CATCH ME IF YOU CAN' . I want to check whether the word ME is inside @mainString . How do I check if a string has a specific substring in SQL? 回答1: CHARINDEX() searches for a substring within a larger string, and returns the position of the match, or 0 if no match is found if CHARINDEX('ME',@mainString) > 0 begin --do something end Edit or from daniels answer, if you're wanting to find a word (and not subcomponents of words), your CHARINDEX call would look like:

Hierarchical Queries in SQL Server 2005

白昼怎懂夜的黑 提交于 2019-12-17 06:45:34
问题 Way back when I was working in an Oracle shop I took the CONNECT_BY for granted. Now I'm stuck working with SQL Server 2005 and have some nasty object hierarchies. Specifically, we have a self referencing table where all child records have a column with their parent's id. Currently we have a view that maps children to levels in the hierarchy and a nasty query that does the heavy lifting to connect parents with their children. While this method works, it is far from elegant and reeks of taint.

SQL Server GUID sort algorithm. Why?

浪尽此生 提交于 2019-12-17 06:37:30
问题 Problem with UniqueIdentifiers We have an existing database which uses uniqueidentifiers extensively (unfortunately!) both as primary keys and some nullable columns of some tables. We came across a situation where some reports that run on these tables sort on these uniqueidentifiers because there is no other column in the table that would give a meaningful sort (isn't that ironic!). The intent was to sort so that it shows the items in the order they were inserted but they were not inserted

How to select date without time in SQL

喜欢而已 提交于 2019-12-17 06:24:21
问题 When I select date in SQL it is returned as 2011-02-25 21:17:33.933 . But I need only the Date part, that is 2011-02-25 . How can I do this? 回答1: I guess he wants a string select convert(varchar(10), '2011-02-25 21:17:33.933', 120) 回答2: For SQL Server 2008: Convert(date, getdate()) Please refer to https://docs.microsoft.com/en-us/sql/t-sql/functions/getdate-transact-sql 回答3: The fastest is datediff, e.g. select dateadd(d, datediff(d,0, [datecolumn]), 0), other.. from tbl But if you only need

Diagnosing Deadlocks in SQL Server 2005

痴心易碎 提交于 2019-12-17 06:22:10
问题 We're seeing some pernicious, but rare, deadlock conditions in the Stack Overflow SQL Server 2005 database. I attached the profiler, set up a trace profile using this excellent article on troubleshooting deadlocks, and captured a bunch of examples. The weird thing is that the deadlocking write is always the same : UPDATE [dbo].[Posts] SET [AnswerCount] = @p1, [LastActivityDate] = @p2, [LastActivityUserId] = @p3 WHERE [Id] = @p0 The other deadlocking statement varies, but it's usually some

Diagnosing Deadlocks in SQL Server 2005

馋奶兔 提交于 2019-12-17 06:22:03
问题 We're seeing some pernicious, but rare, deadlock conditions in the Stack Overflow SQL Server 2005 database. I attached the profiler, set up a trace profile using this excellent article on troubleshooting deadlocks, and captured a bunch of examples. The weird thing is that the deadlocking write is always the same : UPDATE [dbo].[Posts] SET [AnswerCount] = @p1, [LastActivityDate] = @p2, [LastActivityUserId] = @p3 WHERE [Id] = @p0 The other deadlocking statement varies, but it's usually some