SQL Server 2005 drop column with constraints

Here is a script that will delete the column along with its default constraint. Replace MYTABLENAME and MYCOLUMNNAME appropriately. declare @constraint_name sysname, @sql nvarchar(max) select @constraint_name = name from sys.default_constraints where parent_object_id = object_id(‘MYTABLENAME’) AND type=”D” AND parent_column_id = ( select column_id from sys.columns where object_id = object_id(‘MYTABLENAME’) and name=”MYCOLUMNNAME” ) set @sql = N’alter … Read more

Integer division in sql server

In the first you are getting the result of two integers and then casting the result as DECIMAL(9,2). In the second you’re just dividing two integers and that’s expected. If you cast one of the integers as a decimal BEFORE you do the division, you’ll get a decimal result. SELECT 151/CAST(6 AS DECIMAL (9,2))

adding a column description

I’d say you will probably want to do it using the sp_addextendedproperty stored proc. Microsoft has some good documentation on it. Try this: EXEC sp_addextendedproperty @name = N’MS_Description’, @value=”Hey, here is my description!”, @level0type = N’Schema’, @level0name=”yourschema”, @level1type = N’Table’, @level1name=”YourTable”, @level2type = N’Column’, @level2name=”yourColumn”; GO

Foreign key relationship with composite primary keys in SQL Server 2005

Since Table2 has a composite primary key (FileID, FileType), then any reference to it must also include both columns. ALTER TABLE dbo.Table1 ADD CONSTRAINT FK_Table1_Table2 FOREIGN KEY(FileID, FileType) REFERENCES Table2(FileID, FileType) Unless you have a unique constraint/index on the Table2.FileID field (but if so: why isn’t this the PK??), you cannot create a FK relationship … Read more

How to get difference between two rows for a column field?

SELECT [current].rowInt, [current].Value, ISNULL([next].Value, 0) – [current].Value FROM sourceTable AS [current] LEFT JOIN sourceTable AS [next] ON [next].rowInt = (SELECT MIN(rowInt) FROM sourceTable WHERE rowInt > [current].rowInt) EDIT: Thinking about it, using a subquery in the select (ala Quassnoi’s answer) may be more efficient. I would trial different versions, and look at the execution plans … Read more

Unable to create index because of duplicate that doesn’t exist?

It’s not that the index already exists, but that there are duplicate values of the TopicShortName field in the table itself. According to the error message the duplicate value is an empty string (it might just be a facet of posting I guess). Such duplicates prevent the creation of a UNIQUE index. You could run … Read more

Execute Stored Procedure from a Function

EDIT: I haven’t tried this, so I can’t vouch for it! And you already know you shouldn’t be doing this, so please don’t do it. BUT… Try looking here: http://sqlblog.com/blogs/denis_gobo/archive/2008/05/08/6703.aspx The key bit is this bit which I have attempted to tweak for your purposes: DECLARE @SQL varchar(500) SELECT @SQL = ‘osql -S’ +@@servername +’ … Read more