sql-server

What is your biggest SQL Server mistake or embarrassing incident?

You know the one I am talking about. We have all been there at some point. You get that awful feeling of dread and the realisation of oh my god did that actually just happen. Sure you can laugh about it now though, right, so go on and share your SQL Server mishaps with us. Even better if you can detail how you resolved your issue so ...

Self-referencing constraint in MS SQL

Is it true that MS SQL restrict self-referencing constraints with ON DELETE CASCADE option? I have a table with parent-child relation, PARENT_ID column is foreign key for ID. Creating it with ON DELETE CASCADE option causes error "Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths. Specify ON DELETE N...

SQL query giving wrong result on linked server

Hi I'm trying to pull user data from 2 tables, one locally and one on a linked server, but I get the wrong results when querying the remote server. I've cut my query down to select * from SQL2.USER.dbo.people where persId = 475785 for testing and found that when I run it I get no results even though I know the person exists. (persId...

Dynamic Query in SQL Server

Hi, I have a table with 10 columns as col_1,col_2,.... col_10. I want to write a select statement that will select a value of one of the row and from one of these 10 columns. I have a variable that will decide which column to select from. Can such query be written where the column name is dynamically decided from a variable. ...

How to access SQL Server from Cocoa (Mac OS X)?

As the question says: How to access Microsoft SQL Server 2005 from Cocoa on Mac OS X 10.5? The database driver should be free if possible, paid solutions can be OK as well. ...

sp_changeobjectowner

I'm calling SQL's sp_changeobjectowner from C#. I want to check afterwards if it worked. How do I check if a table's owner is who I want it to be? ...

Average a time value in SQL Sever 2005

I've got a varchar field in SQL Sever 2005 that's storing a time value in the format "hh:mm"ss.mmmm". What I really want to do is take the average using the built in aggregate function of those time values. However, this: SELECT AVG(TimeField) FROM TableWithTimeValues doesn't work, since (of course) SQL won't average varchars. Howe...

Equal sign in LINQ

The SQL server doing queries based COLLATE option, so you can define how comparision will be performed (case sensitive or not). You can do it when you creating table or during query execution. How can I control collation during my LINQ to SQL queries? Will my queries be allways case insensitive when I will do table.Column == stringValue...

Table "Inheritance" in SQL Server

Hi, I am currently in the process of looking at a restructure our contact management database and I wanted to hear peoples opinions on solving the problem of a number of contact types having shared attributes. Basically we have 6 contact types which include Person, Company and Position @ Company. In the current structure all of these ...

SQL Cursor w/Stored Procedure versus Query with UDF

I'm trying to optimize a stored procedure I'm maintaining, and am wondering if anyone can clue me in to the performance benefits/penalities of the options below. For my solution, I basically need to run a conversion program on an image stored in an IMAGE column in a table. The conversion process lives in an external .EXE file. Here ar...

What's the difference between WITH CHECK ADD CONSTRAINT followed by CHECK CONSTRAINT and just ADD CONSTRAINT in SQL Server?

I'm looking at the AdventureWorks sample database for SQL Server 2008, and I see in their creation scripts that they tend to use the following: ALTER TABLE [Production].[ProductCostHistory] WITH CHECK ADD CONSTRAINT [FK_ProductCostHistory_Product_ProductID] FOREIGN KEY([ProductID]) REFERENCES [Production].[Product] ([ProductID]) GO ...

How to do conditional SQL Server statements?

I have a database with event information, including date (in MMDDYYYY format). is it possible to write an SQL Server statement to only get rows that fall within a certain time frame? something like this pseudo-statement: SELECT * FROM events WHERE [current_date minus date <= 31] ORDER BY date ASC where date is the date in the SQL Ser...

Going from VisualStudio generated Database stuff to Programmer Generated

I'd like to be able to better access the database so I can execute queries (primarily because I don't understand/know the API for it, but I do know SQL). I don't want to remove everything Visual Studio has done because a lot is already built upon it, but how can I get an object that I can use to execute SQL queries. This is Visual Studi...

Classic ASP: How can I use a value from SQL twice?

I'm very fresh with web dev, but am putting together a simple software catalog with classic ASP. Everything seems fine, except I want to use a value from my SQL database twice on the page. For example, in the page title as well as in the body of the page, however I can only seem to use each value once: .... Set nItem = Request.QuerySt...

Clearing prioritized overlapping ranges in SQL Server

This one is nasty complicated to solve. I have a table containing date ranges, each date range has a priority. Highest priority means this date range is the most important. Or in SQL create table #ranges (Start int, Finish int, Priority int) insert #ranges values (1 , 10, 0) insert #ranges values (2 , 5 , 1) insert #ranges value...

Mass Updates and Commit frequency in SQL Server

My database background is mainly Oracle, but I've recently been helping with some SQL Server work. My group has inherited some SQL server DTS packages that do daily loads and updates of large amounts of data. Currently it is running in SQL Server 2000, but will soon be upgraded to SQL Server 2005 or 2008. The mass updates are running ...

Sql Server 2008 - Difference between collation types

Hi, I'm installing a new SQL Server 2008 server and are having some problems getting any usable information regarding different collations. I have searched SQL Server BOL and google'ed for an answer but can't seem to be able to find any usable information. What is the difference between the Windows Collation "Finnish_Swedish_100" and ...

How to create several stored procedure using SQL

Hi, I have a vb.net function that creates several Stored Procedures based on parameters being passed into the function. I want to move this vb.Net into a single SQL file (for maintenance reasons) but I am not sure how I can re-create it in SQL without creating 7 separate stored procedures. I create a total of 20 Stored Procedures and ...

Using "IN" in a WHERE clause where the number of items in the set is very large

I have a situation where I need to do an update on a very large set of rows that I can only identify by their ID (since the target records are selected by the user and have nothing in common other than it's the set of records the user wanted to modify). The same property is being updated on all these records so I would I like to make a ...

Is it possible to Unit Test SQL (MS SQL Server 2005)?

We are having a lot of tables, views and stuff. There are a few people working on different stuff on the same database and sometimes, we change things that screws up others work (like change the columns in the view, or changing the parameters to the stored procedure). Is there any way, to automate these checks? I don't even know if unit...