sql-server

SQL Server select distinct hell

Hi there, I'm stuck on a problem with sql. I have a table with many duplicate entries with column names like:- eventnumber housenumber value1 value2 None of these column names are the primary key as there are many duplicates. What I would like to do is select into another table by distinct housenumber but I seem to get the whole tabl...

Random Number on SQL without using NewID()

Hello I want to generate a Unique Random number with out using the follow statement : Convert(int, (CHECKSUM(NEWID()))*100000) AS [ITEM] Cause when I use joins clauses on "from" it generates double registers by using NEWID() Im using SQL Server 2000 *PD : When I use Rand() it probably repeat on probability 1 of 100000000 but this ...

Sql Server trigger insert values from new row into another table

I have a site using the asp.net membership schema. I'd like to set up a trigger on the aspnet_users table that inserted the user_id and the user_name of the new row into another table. How do I go about getting the values from the last insert? I can select by the last date_created but that seems smelly. Is there a better way? ...

How to find what Stored Procedures are using what indexes?

I am trying to determine what indexes are no longer used in my Database. I have had great luck using the following query: SELECT OBJECT_NAME(S.[OBJECT_ID]) AS [OBJECT NAME], I.[NAME] AS [INDEX NAME], i.Type_Desc as [Index Type], USER_SEEKS, USER_SCANS, USER_LOOKUPS, USER_UPDATES F...

Dynamic Columns - SQL Server - Months as Columns

DB: SQL Server 2005 We have a table that has data in this manner: Project Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov ...

How to write stored procedure to do this?

I would like to create a stored procedure that takes in a string of comma separated values like this "1,2,3,4", and break it apart and use those numbers to run a query on a different table. so in the same stored procedure it would do something like select somefield from sometable where somefield = 1 select somefield from sometable wher...

How do I move SQL Server error log files to a new location?

Hi, my default SQL Server 2005 log directory is full on C drive. In order to prevent this issue happening in future, I plan to move the default log directory to some other place. Could you please tell me how I can move the error log default directory? I browsed the web, there is solution for SQL Server 7 and 2000 but not 2005. Please ...

When I update/insert a single row should it lock the entire table?

I have two long running queries that are both on transactions and access the same table but completely separate rows in those tables. These queries also perform some update and inserts based on those queries. It appears that when these run concurrently that they encounter a lock of some kind and it’s preventing the task from finishing ...

SQL Server: Query times out when executed from web, but super-fast when executed from SSMS

I'm trying to debug the source of a SQL timeout in a web application that I maintain. I have the source code of the C# code behind, so I know exactly what code is running. I have debugged the application right down to the line that executes the SQL code that times out, and I watch the query running in SQL profiler. When this query ...

Are SQL Server Views processed "before" enclosing query logic

If I have a view, and embed the view in a query, will the view have to be processed fully before the rest of the query? Example: CREATE VIEW dbo.ExpensiveView AS SELECT IndexedColumn, NonIndexedColumn FROM dbo.BigHairyTable WHERE NonIndexedColumn BETWEEN 500000000 AND 500050000 GO SELECT * FROM dbo.ExpensiveView WHERE In...

Why does the sys.indexes table have a NULL Index name?

I ran this query: SELECT i.name AS IndexName, s.used_page_count * 8 AS IndexSizeKB FROM sys.dm_db_partition_stats AS s JOIN sys.indexes AS i ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id WHERE s.[object_id] = object_id('dbo.Stu') ORDER BY i.name and the largest index returned ...

Questions about local database vs service-based database.

I have a some questions about local and service-based databases: Does using a service-based database require the user to have SQL Server installed? If so, is there ANY way around it? Does a local database require the user to have SQL Server installed? What is the difference between a local database and a service-based database. (I am tal...

Dateformat mismatch in sql server and C# code

I have a problem. I have an application in C# with SQL SERVER 2005 as backend. The problem is in fetching the correct record based on the date. The frontend code is if (string.IsNullOrEmpty(txtFromDate.Text)) SelectCmd.Parameters[0].Value = DBNull.Value; else SelectCmd.Parameters[0].Value = txtFromDate.Text; Now if I ru...

Concurrency issues

Hi, Here's my situation (SQL Server): I have a web application that utilizes nHibernate for data access, and another 3 desktop applications. All access the same database, and are likely to utilize the same tables at any one time. Now, with the help of NH I'm batching selects in order to load an aggregate with all of its hierarchy - so...

How to run a sql script using C#

I have a sql script to create a new database which i need to create when our product is installed. For this i need to fire the script using c#. DB is sql-server 2005 express. Plz help.... The sql script is as follows: USE [master] GO /****** Object: Database [Jai] Script Date: 02/12/2010 11:01:25 ******/ CREATE DATABASE [Jai] ON P...

Does anyone have an implementation of sp_helpcolumn?

sp_help lists columns among other things. I am trying to get just the results that include the column information. Has anyone implemented this? ...

SQL Server : Invalid cursor state (0) state why ?

I have a table on which there is update trigger written it has print statement before go statement. ALTER TRIGGER user_type_check ON user_table --code PRINT 'Modification of user is done.' GO SET QUOTED_IDENTIFIER OFF GO SET ANSI_NULLS ON GO Now with this structure whenever i perform update operation on the table it fai...

T-SQL is it possible to use a variable in a select statement to specify the database

Is there a way to do something like this without converting the sql to a string and calling exec DECLARE @source_database varvhar(200) SELECT @source_database = 'wibble' SELECT * FROM SELECT @source_database.dbo.mytable ...

Is there an equivalent to typedef in Transact-SQL?

Is there any way of aliasing datatypes in T-SQL (SQL Server 2005), a bit like a C typedef? I'd like to be able to alias, say, VARCHAR(20) as 'my_datatype' so that wherever I wish to use VARCHAR(20) I could instead use my_datatype. Synonyms allow you to do this sort of thing for tables, and there are built-in synonyms for some datatypes ...

How does a swapping data between two tables in SQL Server work?

I could probably google this, but it seemed quirky enough that it might be worth having logged as an answer on SA. So in development land, if you want to swap interchance the values of two variables, you need a third temp variable. e.g. string x = "ABC"; string y = "DEF"; string temp; temp = x; x = y; y = temp; However in a SQL Up...