tsql

Comparing Query Changes. Is there a better way.

When I am writing queries, I will do so in steps. Sometimes, in the process I will realize that I've made a "mistake" such as ending up with extra or losing records. So, I will typically compare the two queries like so: (Select blah blah blah ) Mine Inner join ((Select blah blah blah ) Orig Where Mine.PK <> Orig.PK or if I'm lookin...

SQL Server: Can I Comma Delimit Multiple Rows Into One Column?

Creating Comma Separated Lists In SQL Hi All, I am attempting to merge something like this in my SQL Server database: [TicketID], [Person] T0001 Alice T0001 Bob T0002 Catherine T0002 Doug T0003 Elaine Into this: [TicketID], [People] T0001 Alice, Bob T0002 Catherine, Doug T0003 ...

Why does my trigger not fire?

I have the following table: if object_id(N'dbo.Node') is null create table dbo.Node ( ID bigint identity primary key, ParentID bigint null foreign key references Node(ID) on delete no action, DateCreated datetime not null, LastUpdated datetime not null, [Name] nvarchar(500) not null, ); Now, because SQL Server comp...

How to find if a word has all vowels (SQL Server 2005)

DECLARE @t TABLE(Words VARCHAR(100)) INSERT INTO @t SELECT 'Stack Overflow' UNION ALL SELECT 'EQUATORIAL' SELECT * FROM @t WHERE Words LIKE '%[AEIOU]%' I am getting both as the output Words Stack Overflow EQUATORIAL The desired output being EQUATORIAL Thanks ...

Sending an SMS through SQL Server 2008

Hi guys, Is there any way to send an SMS through SQL Server 2008? ...

T-SQL to PL/SQL (IDENTITY)

I've got a T-SQL script, that converts field to IDENTITY (in a weird way). How do I convert it to PL/SQL? (and, probably, figure out, if there is a simpler way to do this - without creating a temporary table). The T-SQL script: -- alter table ts_changes add TS_THREADID VARCHAR(100) NULL; -- Change Field TS_ID TS_NOTIFICATIONEVENTS t...

Cannot resolve the collation conflict between "SQL_Latin1_General_Pref_CP1_CI_AS" and "Latin1_General_CI_AS" in the equal to operation.

I have the following query: SELECT DISTINCT(po.SONumber) AS [Sales Order No_], po.PONumber AS PoNo, ph.[Buy-from Vendor No_] AS VendorNo, ph.[Pay-to Name], ph.[Document Date], 'Ship-to Name' = CASE WHEN sh.[Ship-to Name] > '' THEN sh.[Ship-to Name] ELSE sih.[Ship-to Name] END, 'Ship-to Post Code' = CASE WHEN sh.[Ship-to Post Cod...

How am I misunderstanding trigger recursion in TSQL?

I have the following table: if object_id(N'dbo.Node') is null create table dbo.Node ( ID bigint identity primary key, ParentID bigint null, -- references Node(ID) DateCreated datetime not null, LastUpdated datetime not null, [Name] nvarchar(500) not null, ); I have this trigger to achieve cascading deletes within t...

What is the most interesting SQL Question(TSQL Program included) that you encountered?

Actually I am a newbie in SQL SERVER (only 2 months as of now) and want to learn more and master it over the coming days. So I have asked this question. I believe it will help people like me a lot to grow if we follow those that the experienced peoples here have faced or seen so far in their career. Also it will help us in our job or ...

SQL Server: Create Table using first row of Text File as Column names

Hello all, How can I read the first row of a text file (comma separated) and use that as the field names of a new table I want to create in SQL Server? The field names can all be varchar. Thanks all UPDTAE Please note I would like to do this using T-SQL or any other way (sqlcmd etc). I do not want to use the wizard. ...

Database Objects Scripts in Order???

When we script all Sql Server Database objects from Management Studio by right clicking on the database, and go to tasks and select "generate scripts", will the generated scripts be in order??? ...

Hashbytes comparison in stored proceduring not matching record

The password field in my user table (SQL Server 2008) is encrypted using HASHBYTES on insertion. I have a stored procedure with parameters for the username and plain-text password which does a SELECT using that username and the password sent through HASHBYTES, then returns the user record if it finds a match. The SP is always returning...

SQL Server: reasonable "auto increment" techniques?

I'm using SQL Server 2005 for the first time, having mostly worked with MySQL in the past. I'm used to using auto_increment to create unique IDs in tables. Anyway... I'm working in a java app, and need to do the following. Presume my table has two columns: itemID (int) and itemValue(int). This is basically what I want to do (the dbco...

How to speed up a SQL Server query involving count(distinct())

I have a deceptively simple SQL Server query that's taking a lot longer than I would expect. SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT COUNT(DISTINCT(guid)) FROM listens WHERE url='http://www.sample.com/' 'guid' is varchar(64) NULL 'url' is varchar(900) NULL There is an index on guid and url. There are over 7 million ...

SQL - help on "top 1 by group"

Relevant tables: DepartmentPhone: DepartmentPhoneID int, DepartmentID int, PhoneID int Phone: PhoneID int, PhoneType int There are 6 phones with PhoneType=4 that belong to DepartmentID=2. So this produces 6 records: select * from DepartmentPhone join Phone on Phone.PhoneID = DepartmentPhone.PhoneID and Phone.PhoneType = 4 where Depar...

how to get the number on months between two dates in sql server 2005

i have a column in my sql server 2005 table that should hold the number of months an employee has been in service. Since i also have the date the employee was engaged, i want the "months_In_Service" column to be a computed column. now if i use datediff(month,[DateEngaged],getdate()) as the formula for the months in service computed colum...

SQL WITH Statement, Unknown Column in where clause

Hi all, I ve got the following query which is throwing the following error Unkown Column 'RowNum' WITH Employees AS ( SELECT (keyTblSp.RANK * 3) AS [Rank], sp.*, addr.Street, addr.PostOfficeBox, addr.StreetNumber FROM Employee sp INNER JOIN FREETEXTTABLE(Employee, *, 'something', 1000) AS keyTbl...

SQL Recursive Coalesce

Hi all, I'm trying to create a column that contains all cities of the referenced addresses. DECLARE @AddressList nvarchar(max) SELECT @AddressList = COALESCE(@AddressList + ' ', '') + City FROM [Address] SELECT Employee.*, (SELECT @AddressList) AS AddressCities FROM Employee But I dont know where to put the WHERE clause. ...

SQL Server: How does the type of an index affect a join's performance?

If I'm am trying to squeeze every last drop of performance out of a query what affect does having these types of index's being used by my joins. clustered index. non-clustered index. clustered or non-clustered index with extra columns that may not be involved in the join. Will I gain any performance if I go through and create cluster...

SQL Server 2005 Full Text Search over multiple tables and columns

Hi, I'm looking for a good solution to use the containstable feature of the SQL Serve r2005 effectivly. Currently I have, e.g. an Employee and an Address table. -Employee Id Name -Address Id Street City EmployeeId Now the user can enter search terms in only one textbox and I want this terms to be split and search with an "AND" opera...