sql-server-2005

How to emulate a BEFORE DELETE trigger in SQL Server 2005

主宰稳场 提交于 2019-12-23 12:16:39
问题 Let's say I have three tables, [ONE], [ONE_TWO], and [TWO]. [ONE_TWO] is a many-to-many join table with only [ONE_ID and [TWO_ID] columns. There are foreign keys set up to link [ONE] to [ONE_TWO] and [TWO] to [ONE_TWO]. The FKs use the ON DELETE CASCADE option so that if either a [ONE] or [TWO] record is deleted, the associated [ONE_TWO] records will be automatically deleted as well. I want to have a trigger on the [TWO] table such that when a [TWO] record is deleted, it executes a stored

What is the preferred merge method for SQL Server 2005?

帅比萌擦擦* 提交于 2019-12-23 12:08:41
问题 I have mainly been using the Exists Method for merging a row into a table but I am considering switching to the Row Count Method . Is there any reason not to? Exists Method If Exists(Select * From Table Where ID = @ID) Begin Update Table Set Value = @Value Where ID = @ID End Else Begin Insert Into Table (Value) Values (@Value); End Row Count Method Update Table Set Value = @Value Where ID = @ID If (@@RowCount = 0) Begin Insert Into Table (Value) Values (@Value); End Performance The Row Count

VBA Excel To SqlServer

家住魔仙堡 提交于 2019-12-23 12:05:11
问题 What is the best way to write VBA code to connect to SQL Server 2005 from Excel? The users of the excel file might run XP, Vista, Win7 and I want to prevent driver installation as much as possible. My understanding is that XP uses MDAC while Vista/Win7 uses DAC. Does that mean that a reference to MDAC 2.8 will not work on a Vista machine and the other way around? Will my VBA code work on both if I don't add a reference and use late binding, e.g. CreateObject("ADODB.Connection")? 回答1: I've

UNION ALL Performance IN SQL Server 2005

断了今生、忘了曾经 提交于 2019-12-23 12:00:19
问题 I have a query with a long chain of CTEs which ends with SELECT RegionName, AreaName, CityName, SubCityName, StreetName FROM tDictionaryStreets UNION ALL SELECT RegionName, AreaName, CityName, SubCityName, StreetName FROM tDictionaryRegions The execution time of this query is 1450 ms. When I execute these 2 SELECTs separatly it takes much less time. For the query SELECT RegionName, AreaName, CityName, SubCityName, StreetName FROM tDictionaryStreets execution time is 106 ms. And for the query

How to define a regular expression with multiple OR operators where each term includes a space prefix and suffix?

好久不见. 提交于 2019-12-23 12:00:09
问题 I am preparing for a data extraction task. I need to remove a set of terms; none, some or all may be present in each source record string. There are over 100,000 target records. I want to avoid performing single term match/replace actions, since (a) the list of terms-to-be-removed will likely grow, and (b) the time to perform the current match/replace action one term at a time is unacceptable. My question: how do I modify the regular expression to include each term within the OR separated

SQL Server: How to abort a series of batches in Query Analyzer?

☆樱花仙子☆ 提交于 2019-12-23 10:35:22
问题 i have a series of T-SQL statements separated by the special Query Analyzer batch separator keyword: GO If one batch fails, i need Query Analyzer to not try subsequent batches - i want it to stop processing the series of batches. For example: PRINT 'This runs' go SELECT 0/0, 'This causes an error' go PRINT 'This should not run' go Output: This runs Server: Msg 8134, Level 16, State 1, Line 2 Divide by zero error encountered. This should not run Possible? Update An example of this in real use

Data driven business rules.

半腔热情 提交于 2019-12-23 10:27:10
问题 I am using SQL SERVER 2005. I have a table table1(ID,col1,col2,col3,col4); Now I have a business logic like: If col1 >= 126 and col2 > 1 then col3 = 0 if col1 >=126 and col2 < 1 then col3 = col1+col4 Now what I am trying to do is store all these rules in database and make it data driven. THe reason for that is to give the end user more flexibility. If tomorrow the business rules changes, end user have the flexibility to change it through the GUI. For eg. if tomorrow the business wants to

User does not have permission to access a database

谁说我不能喝 提交于 2019-12-23 10:15:12
问题 I'm trying to connect to a database using Windows Authentication. I believe that my current user does not have access to it. How can I enable a user to login to SQL Server, and use the database? 回答1: You need to use the SQL Server Management Studio program to grant access for the user. You'll need to connect in with a login that has administration privileges for the database. If you have don't have those privileges you'll need to contact someone that does. If you do have a login with those

Sorting based on next and previous records in SQL

喜欢而已 提交于 2019-12-23 09:44:01
问题 I am trying to order a specific query by taking the next and previous records into account, but I can't seem to get it done. I would like to order by a number and a letter, but if, for example, the last letter of number 1 is equal to one of the letters of number 2, I want to change the ordering, so that the letter matches with the following record. Create script and SQL fiddle demo create table Parent ( id [bigint] IDENTITY(1,1), number bigint NOT NULL, PRIMARY KEY (id) ) GO create table

Order by and Different Types in a CASE

删除回忆录丶 提交于 2019-12-23 09:29:55
问题 I have a requirement to order a result set by a column, based on an Integer input into a parameter. Problem is, I need to use a CASE for the OrderBy, and it seems the code accepts the first 'TYPE' in the case column... any other types fail. My code is like this: WITH error_table AS ( SELECT Row_Number() OVER (ORDER BY CASE @orderBy WHEN 1 THEN received_date -- Last Rx'd message WHEN 2 THEN message_id -- Message Id WHEN 3 THEN zibmat.short_name -- Message action type WHEN 4 THEN error_action