tsql

sliding window scenario - taking partions offline

Is there a way in MS SQL Server 2005 to take a partition / file groups / files offline? I have a lot of data in certain tables and would like to use the sliding window Scenario: http://msdn.microsoft.com/en-us/library/ms345146%28SQL.90%29.aspx#sql2k5parti%5Ftopic24 Instead of keeping all the data in the first partition, I would like to...

Dropping and recreating tables... to avoid indexing issues?

I'm running into an interesting argument with a DBA who insists that the DELETE command should not be used in T-SQL, that instead the proper way to remove data is to move the data you'd like to keep to a temp table, and drop and re-create the original table. The justification given for this is that it should prevent index fragmentation i...

Should NULLS be handled in code or in the database? Advantages and Disadvantages?

I have several questions regarding where to handle nulls. Let me set up a scenario. Imagine I have a table that has 5 varchar(50) columns to use as an example when providing reasons for using nulls or empty strings. Is it better to handle NULLS in code or in the database? By this I mean, is it better to assign an empty string to a var...

Counts for lots of boolean fields in one sql query?

Not sure exactly how to explain this but Imaging you have a table with lots of boolean fields like this...... Table: Cars Columns: Automatic: boolean Silver: boolean American: boolean Noisy: boolean Smelly: boolean fast: boolean (silly fields and most of them wouldn't be bools in reality but just an examp...

Fast way to eyeball possible duplicate rows in a table?

Similar: http://stackoverflow.com/questions/91784 I have a feeling this is impossible and I'm going to have to do it the tedious way, but I'll see what you guys have to say. I have a pretty big table, about 4 million rows, and 50-odd columns. It has a column that is supposed to be unique, Episode. Unfortunately, Episode is not unique...

SQL Date Format

How do I format a data in SQL to read like this: Monday, November, 23 2009 ...

Find last sunday

How will you find last sunday of a month in sql 2000? ...

dispalying error returned by stored procedure in .aspx form

hey i have a stored procedure which i use to insert values in a table...if i implement error handling in it using @@ERROR and if i it returns error in a variable @myError can i display this error in my .aspx form??i m using sql server 2005 thanx.... ...

T-SQL stored procedure returns null in code, but works in console

I have a stored procedure CREATE procedure [dbo].[get_unique_identifier] AS DECLARE @ret_val INT UPDATE seq SET @ret_val = id = id + 1 RETURN @ret_val that queries a table (seq) that has a single int column and single row, increments the value and then then returns it. Don't ask why I'm doing this, but in short, the idea is to simulat...

JOIN over multiple columns?

Hi folks, I have a database-table with the following structure: id1 | id2 | weight Now I want to sum up the weight for all entries, that have in id1 or id2 a given value. SELECT id, SUM(weight) FROM myTable WHERE id1=@id or id2=@id Usually, I need to get the sum for more than one id. To speed things up, I first load the ids for wh...

How to keep an audit/history of changes to the table

I've been asked to create a simple DataGrid-style application to edit a single table of a database, and that's easy enough. But part of the request is to create an audit trail of changes made, who made them, and the date/time. How might you solve this kind of thing? (I'll be using C# in VS2008, ADO.NET connected to SQL Server 2005, WPF...

T-SQL STR() C#.NET LINQ to SQL Equivalent

Hello, I'm trying to convert a standard SQL query to a LINQ to SQL query. I was wondering what the LINQ equivalent of the T-SQL function STR() is? STR (T-SQL) Returns character data converted from numeric data. Syntax STR(float_expression[, length[, decimal]]) Arguments float_expression Is an expression of app...

Is it possible to restore a SQL Server database from a virtual drive?

In SQL Server Management Studio, if I attempt to restore a database from a backup file with a command like: RESTORE DATABASE somedatabase FROM DISK = '<virtual disk>:\<path>\<backup file>'; I get a "file not found" error. I'm creating with the "subst" command. But if I replace with a physical disk and with the complete path, the ...

TSQL- generate a sequence number for duplicate records

Using SQL Server 2000, consider a source table with more than 400,000 records. The task is to select each regno entry with an incrementing on-the-fly rowid or sequence number for those with duplicates or multiple entries. For those which do NOT have duplicate entries in the source table, the rowid should simply be null. Here's an exam...

How to use variable in target database name for insert statement?

I want to declare a server name and use this name in an insert statement. So far all i got is an error message. declare @machine nvarchar(6); declare @bar nvarchar(3); set @machine = 'Name00'; set @bar = 'foo' insert into @machine.dbname.dbo.table (column1, column2) select (column1, column2) from table where column1 = @bar This gives...

MS SQL: How to separate out records which have no childs in the same table?

Hi everyone! I have a tricky problem. I have a very complex view in MS SQL 2005 with the following result: | ID | Name | ParentID | -------------------------------- | 1 | Cars | 1 | | 2 | Audi | 1 | | 3 | Toyota | 1 | | 4 | Trucks | 4 | Now I want my view to recogniz...

Need some help with a SQL Query

Hi all, I need some help with a SQL query for SQL Server 2005. Here is an example of the data in the table to be queried: Id PersonId PayrollNum ContractId PayrollFrom PayrollTo --------------------------------------------------------------------------- 1 432642 85110892 1 01/05/2009 ...

How do I exclude Weekend days in a SQL Server query?

How do I exclude values in a DateTime column that are Saturdays or Sundays? For example, given the following data: date_created '2009-11-26 09:00:00' -- Thursday '2009-11-27 09:00:00' -- Friday '2009-11-28 09:00:00' -- Saturday '2009-11-29 09:00:00' -- Sunday '2009-11-30 09:00:00' -- Monday this is the result I'm looking for: d...

How do you concatenate strings inside of a CONTAINS in SQL Server 2008?

SQL Server 2008 is telling me that it doesn't like the "+" in the CONTAINS. Not sure what I'm doing wrong here. INSERT INTO dbo.tblImportTitles ( ImportTitleGUID, UserGUID, TitleName, TitleGUID ) SELECT ImportTitleGUID = T.Item.value('@ImportTitleGUID', 'uniqueidentifier'), UserGUID = T.Item.value('@UserGUID', 'u...

How can I parse a string in sql?

Hi I am passing a string in a stored procedure (sql Server 2000) like this "123456788997877" (please note I dont have delimited pipes etc...) I need to do some calculations for each number in the string. How do I loop each number in a given string and do something about it? Thanks a lot ...