tsql

SQL Server 2005: File Resizing

Hi all, I have some databases that have four files each; one for PRIMARY, IDX, IMAGE, LOG their initial sizes were set to 200MB each but they are consuming far less space, about 100MB total. 1) is it possible to resize the database files while they are in use down to more reasonable sizes, say "current consumption" + 10MB? 2) is it p...

Find Number of Unique Values in Table

I have a table with four columns: PartNumber, ValvePartNumber, ActuatorPartNumber, Price I want to find the number of distinct prices for each combination of ValvePartNumber and ActuatorPartNumber. This is using SQL Server 2005 ...

Getting week number off a date in MS SQL Server 2005?

Is it possible to create an sql statement that selects the week number (NOT the day of week - or the day number in a week). I'm creating a view to select this extra information along with a couple of other fields and thus can not use a stored procedure. I'm aware that it's possible to create a UDF to do the trick, but if at all possible ...

Start stored procedures sequentially or in parallel

We have a stored procedure that runs nightly that in turn kicks off a number of other procedures. Some of those procedures could logically be run in parallel with some of the others. How can I indicate to SQL Server whether a procedure should be run in parallel or serial ie: kicked off of asynchronously or blocking? What would be...

What's the shortest TSQL to concatenate a person's name which may contain nulls

3 fields: FirstName, MiddleName, LastName Any field can be null, but I don't want extra spaces. Format should be "First Middle Last", "First Last", "Last", etc. ...

How to trace T-SQL function calls

I'm trying to debug a rather complicated formula evaluator written in T-SQL UDFs (don't ask) that recursively (but indirectly through an intermediate function) calls itself, blah, blah. And, of course, we have a bug. Now, using PRINT statements (that can then be read from ADO.NET by implementing a handler for the InfoMessage event), I ...

Need to speed up this query in SQL Server

SELECT pe.prodtree_element_id prodID, pe.prodtree_element_name_s, li.line_name, av2.value FROM prodtree_element pe LEFT JOIN prodtree_link pl ON pe.prodtree_element_id = pl.to_prodtree_node_id LEFT JOIN line li ON pe.line_code = li.line_code INNER JOIN attribute_values av ON av.attribute_definition_id = #st...

How do I grant limited access to sysprocesses in SQL Server 2005?

I'm implementing some rudimentary SQL Server monitoring to watch for excessive blocking. I have a stored procedure that looks like this: CREATE PROCEDURE [dbo].[bsp_GetBlockedProcessCount] AS BEGIN SET NOCOUNT ON; DECLARE @count INT SELECT @count = COUNT(*) FROM master.dbo.sysprocesses WITH (NOLOCK) WHERE blocked ...

Exception of type 'System.OutOfMemoryException' was thrown. Why?

I have a dynamic query that returns around 590,000 records. It runs successfully the first time, but if I run it again, I keep getting a System.OutOfMemoryException. What are some reasons this could be happening? The error is happening here: public static DataSet GetDataSet(string databaseName,string ...

Can you call ROW_NUMBER() in dynamic sql and use Common Table Expressions in Dynamic Sql?

I want to limit the number of records returned by calling ROW_NUMBER(), but I am curious as to if you can call it in dynamic sql? Also, Can you use CTE's in dynamic sql? ...

What is wrong with this query? Can't get ROW_NUMBER() to work.

SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY hrl.Frn) as Row, hrl.unq, hrl.LcnsId, hc.Business,hc.Name,hc.Phone, hrl.Frn,hrl.CallSign, hrl.gsamarkettypeid, gmt.[Market Type Code] + ' - ' + gmt.gsamarkettype, hrl.gsalatitude,hrl.gsalongitude, rsc.RadioServiceCode + ' - ' + rsc.RadioService, GrantDt, Ex...

Is T-SQL's CASE normal ("lazy") evaluation?

Is T-SQL's CASE lazy evaluation? ...

Query to find all the tables with date columns in it

Hi, Can anybody let me know the query to find all the tables that has a date columns on it. Thanks ...

Best way to update table schema for huge tables (SQL Server)

I have a few huge tables on a production SQL 2005 DB that need a schema update. This is mostly an addition of columns with default values, and some column type change that require some simple transformation. The whole thing can be done with a simple "SELECT INTO" where the target is a table with the new schema. Our tests so far show tha...

How Do I Change the File Naming Convention for Scripted Objects Files in SQL2005?

SQL Server 2005 Express Edition SP2 supports scripting of objects into separate files. By default, the filenames include the name of the object's owner, the name of the SQL object followed by the object type. For example, dbo.Employee.Table.sql or dbo.GSP_EmployeeUpdate.StoredProcedure.sql. Though this naming convention may help one to...

T-SQL Background Processing

I'm having the trouble finding the wording, but is it possible to provide a SQL query to a MS SQL server and retrieve the results asynchronously? I'd like to submit the query from a web request, but I'd like the web process to terminate while the SQL server continues processing the query and dumps the results into a temp table that I ca...

Use of SET ROWCOUNT in SQL Server - Limiting result set

I have a sql statement that consists of multiple SELECT statements. I want to limit the total number of rows coming back to let's say 1000 rows. I thought that using the SET ROWCOUNT 1000 directive would do this...but it does not. For example: SET ROWCOUNT 1000 select orderId from TableA select name from TableB My initial thought wa...

Version Agnostic SQL Server Script/Statement that detects existence of a specific database

Due to the packaged nature of the release, a SQL Server script (well more of a statement) needs to be created that can execute correctly on SQL Server 7.0 thru 2008 which can essentially achieve this: if exists(select * from sys.databases where name = 'Blah') Reasons this is difficult. SQL 7 'sys.databases' is not valid SQL 2008 'sys...

BULK IMPORT a zip file in T-SQL

I've got some data files that are stored compressed on our company's server with a .Z extension (UNIX compress utility used to zip them down). Can SQL Server's BULK IMPORT operation read these files in that format? Or must I uncompress them before getting at the data? ...

How do I kill an unruly spid in Sybase?

I've got a couple of rogue spid's in my database that I can see are sleeping when I log in as sa and use sp_who, but attempts to use kill <spid> to eliminate them have failed and I don't actually have access to the server itself to bounce it. Is there any other way of killing these things off? ...