tsql

Why would you NOT set IGNORE_DUP_KEY to ON?

IGNORE_DUP_KEY = ON basically tells SQL Server to insert non-duplicate rows, but silently ignore any duplicates; the default behavior is to raise an error and abort the entire transaction when there are duplicates in a column that doesn't allow them. I've worked with a ton of data that normally has at least one duplicate when there shou...

Implode type function in SQL Server 2000?

Is there an Implode type function for SQL Server? What I have is a list (in a SQL server table): Apple Orange Pear Blueberry and I want them to come out as Apple, Orange, Pear, Blueberry I hope for the space and comma to be configurable but I can always replace it if it isn't... Quick help would be appreciated! ...

LINQ Join On Between Clause

Hello All, I am having some issues throwing together a LINQ query that will join a table based on a zip code. I need to join the table based on whether the customer's zip code lies with in a range of zip codes that is determined by ZIPBEG and ZIPEND columns. The T-SQL would look something like this: JOIN [ZipCodeTable] [zips] O...

MSSQL2005: Restore database without restoring full text catalog

When restoring my database i have a problem with the physical file of the full text catalog being in use. The file 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\FTData\MyCatalog' cannot be overwritten. It is being used by database 'demo2'. I use this restore statement RESTORE database demo from disk = N'c:\temp\demo.bak' WIT...

SQL SERVER 2005: Drop all the tables, stored procedures, triggers, constriants and all the dependencies in one sql statement.

Hi, Is there any way in which i can clean a db in SQl Server 2005 i.e by dropping all the tables and deleting stored procedures, triggers, constraints and all the dependencies in one sql statement. REASON FOR REQUEST: I want to have a db script for cleaning up an existing db which is not in use rather than creating new ones, specially ...

What is the best way to return an error from a TSQL Proc?

Here’s the scenario: You load a page, which renders based on the data in the database (call these the “assumptions”) Someone changes the assumptions You submit your page ERROR! The general pattern to solve this problem is this (right?): In your save proc, inside a begin and commit transaction, you validate your assumptions first. ...

How to select distinct elements when they appear repeated in a table? (T-SQL)

Consider the following table: create table temp ( name int, a int, b int ) insert into temp (name, a, b) values (1, 2, 3) insert into temp (name, a, b) values (1, 4, 5) insert into temp (name, a, b) values (2, 6, 7) I want to select *(all fields) with distinct [name]. In the case of two or more rows having the ...

T-SQL to trim a datetime to the nearest date?

Duplicate of http://stackoverflow.com/questions/2775/whats-the-best-way-to-remove-the-time-portion-of-a-datetime-value-sql-server I have a column that tracks when things are created using a datetime, but I'd like to generate a report that groups then by day, so I need a way of nulling out the time component of a datetime column. How do...

Delete backup file in transact-sql during database copy/restore

I have a "copy" stored procedure for a database. During the copy, I backup the database with something like: exec('backup database [' + @source + '] ' + 'to disk = N''' + @backupdir + '\copybackup'' ' + 'with noformat, noinit, name=N''copybackup-full'', ' + 'SKIP, NOREWIND, NOUNLOAD, STATS = 10;'); And then create and emp...

How to compare string data to table data in SQL Server - I need to know if a value in a string doesn't exist in a column

I have two tables, one an import table, the other a FK constraint on the table the import table will eventually be put into. In the import table a user can provide a list of semicolon separated values that correspond to values in the 2nd table. So we're looking at something like this: TABLE 1 ID | Column1 1 | A; B; C; D TABLE 2 ID ...

how to create xml from xml fragments

I am using SQL Server 2005 and I have a XML Data Type column that stores xml fragments like so in one big table: row 1 ..... <Order date='2009-02-11' customerID='4' /> row 2...... <OrderItem OrderID='6' ItemID='477' quantity='1' /> I would like to create an XML using T-SQL that looks like this from these nodes: <Orders> <Order data...

SQL 2005 T-SQL programming - possible pitfalls to be aware of

You might be aware of the Halloween problem in which a an SQL cursor can be a nightmare for you if not properly used. What are the other subtle problems (difficult to debug and detect) that might be introduced if one does not carefully make use of SQL 2005 features? ...

Query Optimization TSQL and otherwise

Assume I've a sql statement like the following with the variable @FOO set somewhere earlier in code: SELECT FIELDLIST FROM TABLE WHERE (FIELD = @FOO OR @FOO IS NULL) Is the query optimizer smart enough to do the second side of the OR first (@FOO IS NULL) because (another assumption) it is faster to do a null check than it is to d...

How is a T-SQL transaction not thread-safe?

The following (sanitized) code sometimes produces these errors: Cannot drop the table 'database.dbo.Table', because it does not exist or you do not have permission. There is already an object named 'Table' in the database. begin transaction if exists (select 1 from database.Sys.Tables where name ='Table') begin d...

Please help me shorten this t-sql query...

I need to shorten this query and while I'm pretty good at SQL, I'm still learning. SELECT 'doejoh', DATETIME, [Recipient-Address], [Message-Subject], [Sender-Address] FROM dbo.Logs WHERE LEFT([Recipient-Address], 6) IN ('doejoh') UNION ALL SELECT 'doejoh', DATET...

Confused how this query works

I'm trying to learn advanced sql and how to use system queries (sql server). The below query is a little confusing. CREATE PROC dbo.ShowHierarchy ( @Root int ) AS BEGIN SET NOCOUNT ON DECLARE @EmpID int, @EmpName varchar(30) SET @EmpName = (SELECT EmpName FROM dbo.Emp WHERE EmpID = @Root) PRINT REPLICATE('-', @@NEST...

What are the implications of having out of date table statistics on a Sybase/SQLServer database?

For example, for heavily used tables with volumes in the order of 10 million rows that grow by a million rows a month, if the stats are 6-8 months old how detrimental to the performance of the database is this going to be? How often should you be refreshing the stats? ...

C# Using LIKE to see if X contains Y

Hi All, I've inherited the code below but I'm puzzled as the code works just fine but I'm not convinced it's correct. I've not used LIKE before so I can't decide if I should bother changing this. How does this look to you? The code is for a filter in a db it checks if X value contains Y value, find X* in X (e.g. Match would be F* in Fu...

From a Query Window, can a Stored Procedure be opened into another Query Window?

Is there command inside a Query Window that will open a stored procedure in another Query Window? i.e. MODIFY dbo.pCreateGarnishmentForEmployee I am using SQL Server management Studio 2005 and Red Gate's SQL Prompt. Currently I have to do the follwowing multiple steps: Open Object Explorer Navigate Programmability | Stored Procedure ...

Ways to Search for a Pattern in all Stored Procedures and then Open it to be Altered

How can you search all the Stored Procedures for a Pattern and then open the stored procedures to be edited? Is there anything built inside of SQL Server 2005? Or are there any 3rd party addins that will do this searching? I am also using Red Gate's SQL Prompt but I have not noticed that option. Currently I am using the following co...