sql-server-2005

SQL Server unique auto-increment column in the context of another column

 ̄綄美尐妖づ 提交于 2019-12-22 02:29:47
问题 Suppose the table with two columns: ParentEntityId int foreign key Number int ParentEntityId is a foreign key to another table. Number is a local identity, i.e. it is unique within single ParentEntityId . Uniqueness is easily achieved via unique key over these two columns. How to make Number be automatically incremented in the context of the ParentEntityId on insert? Addendum 1 To clarify the problem, here is an abstract. ParentEntity has multiple ChildEntity , and each ChiildEntity should

Cannot assign a default value to a local variable in SQL

强颜欢笑 提交于 2019-12-22 01:37:38
问题 I am trying to declare local variable like: DECLARE @thresholdDate DATETIME = '2014-11-30' And I am getting error: Cannot assign a default value to a local variable. As per documentation: DECLARE @find varchar(30); /* Also allowed: DECLARE @find varchar(30) = 'Man%'; */ What I am doing wrong? 回答1: Prior to SQL Server 2008, assigning a default value (or initial value) to a local variable is not allowed; otherwise this error message will be encountered. Solution 1: (Use SET ) DECLARE

Have you ever encountered a query that SQL Server could not execute because it referenced too many tables?

▼魔方 西西 提交于 2019-12-22 01:27:37
问题 Have you ever seen any of there error messages? -- SQL Server 2000 Could not allocate ancillary table for view or function resolution. The maximum number of tables in a query (256) was exceeded. -- SQL Server 2005 Too many table names in the query. The maximum allowable is 256. If yes, what have you done? Given up? Convinced the customer to simplify their demands? Denormalized the database? @(everyone wanting me to post the query): I'm not sure if I can paste 70 kilobytes of code in the

SQL Server 2000/2005 identity column + replication

感情迁移 提交于 2019-12-22 01:18:04
问题 I have looked at some resources already and just want to clarify and get an opinion. First of all to totally avoid any problems we could just not bother using identity columns as primary keys instead have them generated ourselves and just have those values replicated both ways presuming they are always unique at any time of creation. For the purposes of this question I am talking about 2 or more way replication to solve global access issues and we do have identity columns. Now we are setting

I have a Tags Table. How to Bulk Insert using LINQ?

∥☆過路亽.° 提交于 2019-12-22 00:24:46
问题 I am using VB.NET with LINQ to MS SQL. I have two following tables. Now I want to insert multiple items in Tags Table using LINQ, but also wants to check that if any of tag exists in Tags table. It doesn't insert it again and bring the TagID in both cases (if inserted or if found existed) CREATE TABLE Tags (TagID bigint not null , Tag varchar(100) NULL ) CREATE TABLE VideoTags (VideoID bigint not null , TagID bigint not null ) What's the best way to acheive this using LINQ? Thanks in advance

SET NOCOUNT ON brings back messages in SQL Server Management Studio

我的未来我决定 提交于 2019-12-21 23:30:36
问题 I have the following stored procedure: ALTER PROCEDURE [dbo].[spTitle_GetTitleById] ( @TitleId INT ) AS BEGIN SET NOCOUNT ON; SELECT Id, Name, Active FROM Title WHERE Id = @TitleId END I was told to use SET NOCOUNT ON; if I don't want messages to be returned. I ran this stored procedure through SQL Server Management Studio 2008 and I got the following message: (1 row(s) affected) This is still a message. One of our DBAs said that this will be the case, but when it is run through an

stored procedure in SQL CLR

試著忘記壹切 提交于 2019-12-21 23:22:03
问题 How do you write a stored procedure using C# in SQLCLR? Currently I am using SQL Server Management Studio and I write stored procedures using T-SQL such as create proc sp_item as .... . 回答1: See: Building my first SQL CLR stored procedure CLR Assembly RegEx Functions for SQL Server by Example Choosing between CLR and T-SQL stored procedures: a simple benchmark Basically, there are Visual Studio templates which allow you to get started with SQL CLR projects. Fill in the blanks, write your

Row Locator in Non Clustered Index

£可爱£侵袭症+ 提交于 2019-12-21 21:03:08
问题 I was reading about Non Clustered Index which says that " Nonclustered index contain only the values from the indexed columns and row locators that point to the actual data rows, rather than contain the data rows themselves. This means that the query engine must take an additional step in order to locate the actual data." Query - I am not clear with Row Locator . I am assuming that it is not any Primary key . There is something happening in background which has to do with Row-Locator to

How can I join an XML column back onto the record it originates from?

我怕爱的太早我们不能终老 提交于 2019-12-21 20:57:20
问题 I have a table "Blah" with a PK column BlahID and an XML column BlahItems in a database on SQL Server 2005. This table has records as follows... BlahID BlahItems ------ ------------------------------------------------------ 1 <root><item name="Fred" /><item name="Wilma" /></root> 2 <root><item name="Lisa" /><item name="Bart" /></root> How can I query that table to produce the following.... BlahID BlahItem ------ -------- 1 Fred 1 Wilma 2 Lisa 2 Bart The closest I've managed to get is on a per

Find last record in a single-table chain (SQL Server)

你说的曾经没有我的故事 提交于 2019-12-21 20:55:08
问题 Got this table in SQL Server 2005, which is used to maintain a history of merging operations: Column FROM_ID (int) Column TO_ID (int) Now I need a query that takes the original FROM_ID as input, and returns the last available TO_ID. So for instance: ID 1 is merged to ID 2 Later on, ID 2 is merged to ID 3 Again later, ID 3 is merged to ID 4 So the query I'm trying to put together will take as input (in the WHERE clause I presume) ID 1, and should give me the last available TO_ID as a result,