sql-server

SQL query to compare product sales by month

I have a Monthly Status database view I need to build a report based on. The data in the view looks something like this: Category | Revenue | Yearh | Month Bikes 10 000 2008 1 Bikes 12 000 2008 2 Bikes 12 000 2008 3 Bikes 15 000 2008 1 Bikes 11 000 2007 ...

What is the easiest way using T-SQL / MS-SQL to append a string to existing table cells?

I have a table with a 'filename' column. I recently performed an insert into this column but in my haste forgot to append the file extension to all the filenames entered. Fortunately they are all '.jpg' images. How can I easily update the 'filename' column of these inserted fields (assuming I can select the recent rows based on known i...

Why does Guid.ToString() reverse the byte order?

We're storing some Guids in a MS SQL database. There's some legacy code that does Guid.ToString() and then passes them in to a varchar(64) and there's some newer code that passes them in using a uniqueidentifier parameter. When you look at the results using MS SQL Management studio they look different. The byte order of the first three b...

SQL/Query tools?

I haven't really done a ton of database work in a few years and have become unaccustomed with the tools available. Back a few years ago I was working with oracle databases and was using primarily TOAD with a little bit of MS Access as my tools of choice to prototype the queries I was using in my applications. I really loved TOAD in tha...

What is a good book on MySql for someone experienced with SQL Server?

I am about to migrate a project from SQL Server to MySQL. I am looking for a good book that will quickly get me up to speed with MySQL (that assumes I already have a good knowledge of relational databases). Any recommendations? ...

What's the ASP.NET Connection String Format for a Linked Server?

I've got a database server that I am unable to connect to using the credentials I've been provided. However, on the staging version of the same server, there's a linked server that points to the production database. Both the staging server and the linked server have the same schema. I've been reassured that I should expect to be able to...

Good SQL Server 2005/2008 References

What are some good references for SQL Server 2005/2008? As someone with background working in PHP and interacting with MySQL/PostGreSQL databases, is there anything specific that I should be on the look out for? ...

Best way to learn SQL Server

So I'm getting a new job working with databases (Microsoft SQL Server to be precise). I know nothing about SQL much less SQL Server. They said they'd train me, but I want to take some initiative to learn about it on my own to be ahead. Where's the best place to start (tutorials, books, etc)? I want to learn more about the SQL languag...

How do I generate scripts that will rebuild my MS SQL Server 2005 database WITH data?

I have a SQL Server 2005 database that I would like to be able to recreate at a moment's notice. I want to be able to point to my database and generate a complete set of scripts that will not only create all the tables/views/sprocs/functions that are in the database, but will also populate all the tables with data. Are there any tools t...

SQL Server 2005 insert trigger not inserting enough records

I have a table in a SQL Server 2005 database with a trigger that is supposed to add a record to a different table whenever a new record is inserted. It seems to work fine, but if I execute an Insert Into on the master table that uses a subquery as the source of the values, the trigger only inserts one record in the other table, even thou...

Recommend a tool to manage Extended Properties in SQL server 2005

Server Management Studio tends to be a bit un-intuitive when it comes to managing Extended Properties, so can anyone recommend a decent tool that improves the situation. One thing I would like to do is to have templates that I can apply objects, thus standardising the nomenclature and content of the properties applied to objects. Many ...

How to get a number from a sql string range

I have a column of data that contains a percentage range as a string that I'd like to convert to a number so I can do easy comparisons. Possible values in the string: '<5%' '5-10%' '10-15%' ... '95-100%' I'd like to convert this in my select where clause to just the first number, 5, 10, 15, etc. so that I can compare that value to a pa...

What's the difference between a Table Scan and a Clustered Index Scan?

Since both a Table Scan and a Clustered Index Scan essentially scan all records in the table, why is a Clustered Index Scan supposedly better? As an example - what's the performance difference between the following when there are many records?: declare @temp table( SomeColumn varchar(50) ) insert into @temp select 'SomeVal' selec...

How to create a new instance of Sql Server 2005

I forgot my password for Sql Server 2005. Windows Authentication is not enabled so I cannot login. How can I remove the current instance and create a new db instance? Or is there a better solution exists? ...

SQL - What are your favorite performance tricks?

When you have a query or stored procedure that needs performance tuning, what are some of the first things you try? ...

SQL - How can I remove duplicate rows?

What is the best way to remove duplicate rows from a fairly large table (i.e. 300,000+ rows)? The rows of course will not be perfect duplicates because of the existence of the RowID identity field. MyTable ----------- RowID int not null identity(1,1) primary key, Col1 varchar(20) not null, Col2 varchar(2048) not null, Col3 tinyint not ...

How do I do an Upsert Into Table?

I have a view that has a list of jobs in it, with data like who they're assigned to and the stage they are in. I need to write a stored procedure that returns how many jobs each person has at each stage. So far I have this (simplified): DECLARE @ResultTable table ( StaffName nvarchar(100), Stage1Count int, Stage2Count int ) INS...

Trigger without a transaction?

Is it possible to create a Trigger that will not be in a transaction? I want to update data on a linked server with a trigger but due to firewall issues we can't create a distributed transaction between the two servers. ...

PHP with SQL Server 2005+

Currently we have a hybrid ASP/PHP setup connecting to a SQL Server 2005 database. But all the query work is done on the client side, I'd like to move some of this to PHP. What driver and/or connection string is needed to connect to Sql Svr and what is the syntax to use in PHP? Update: OK so I was definitely trying to avoid using an...

Diagnosing Deadlocks in SQL Server 2005

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] S...