tsql

How to track number of changes occured in a column? T-SQL - SQL Server

For example, i have a column named EmployeeName. Every time a user changes/fix his name, i need to keep a count. If he changes his name twice, then count is 2. Also, i need to store the time of every change employee makes on EmployeeName e.g. if the name essentially is James and time created is 9:00 AM and then employee changes to John ...

sql raiseerror error.number wrong in VB

I wrote a T-SQL query which includes a test for valid EmployeeNo. If the EmployeeNo is not valid, I do the following: RAISERROR(5005, 10, 1, N'Invalid Employee No') return @@Error Back in VB.Net I test the sql exception and found that when the Employee No is invalid the error.number is not 5005 as I would expect, but 2732. What is th...

SQL SERVER FOR XML SYNTAX

How can I get an output as follows using FOR XML / sql query. I am not sure how I can get the Column Values as Elements instead of the tables' column Names. I am using sql server 2005 I HAVE TABLE SCEMA AS FOLLOWS CREATE TABLE PARENT ( PID INT, PNAME VARCHAR(20) ) CREATE TABLE CHILD ( PID INT, CID INT, CNAME VARCHAR(20) ) CREATE...

Tsql can't update the column with count statement

declare @t1 Table ( a1 int ) insert into @t1 select top 10 AnimalID from Animal --select * from @t1 declare @t2 table ( dogs int null ) update @t2 set dogs = (Select COUNT(*) from @t1) ---------> The out put it gives me is just 0 ...

t-sql recursive query

Based on an existing table I used CTE recursive query to come up with following data. But failing to apply it a level further. Data is as below id name parentid -------------------------- 1 project 0 2 structure 1 3 path_1 2 4 path_2 2 5 path_3 2 6 path_4 3 7 path_5 4 8 path_6 ...

Get Unique Results in a query.

Hi guys! Im sure this will be pretty simple for a t-sql guru. I have the following result from a table idMain IdSecondary TextValue 1 1 text1 1 2 text2 2 5 text3 2 6 text5 And I want to obtain the first occurence of the idMain only. The result should be like this....

I assume Row_Number doesn’t act only on rows of the window frame

a) Quote is taken from http://www.postgresql.org/docs/current/static/tutorial-window.html for each row, there is a set of rows within its partition called its window frame. Many (but not all) window functions act only on the rows of the window frame, rather than of the whole partition. By default, if ORDER BY is supplied then the fra...

integer Max value constants in SQL Server T-SQL?

Are there any constants in T-SQL like there are in some other languages that provide the max and min values ranges of data types such as int? I have a code table where each row has an upper and lower range column, and I need an entry that represents a range where the upper range is the maximum value an int can hold(sort of like a hackis...

Multi-base conversion - using all combinations for URL shortener

I am making an URL shortener, and I am struggling with the optimal way of encoding a number (id) into a character string. I am using the characters 0-9,A-Z,a-z so it will basically be a base-62 encoding. That is pretty basic, but it doesn't make use of all possible codes. The codes that it would produce would be: 0, 1, ... y, z, 10, 11...

SQL Server 2005 - query with case statement

Trying to put a single query together to be used eventually in a SQL Server 2005 report. I need to: Pull in all distinct records for values in the "eventid" column for a time frame - this seems to work. For each eventid referenced above, I need to search for all instances of the same eventid to see if there is another record with TaskN...

Replace with wildcard, in SQL

I know MS T-SQL does not support regular expression, but I need similar functionality. Here's what I'm trying to do: I have a varchar table field which stores a breadcrumb, like this: /ID1:Category1/ID2:Category2/ID3:Category3/ Each Category name is preceded by its Category ID, separated by a colon. I'd like to select and display t...

SqlServer2008 - Can I Alter a Scalar Function while it is referenced in many places

We have a scalar function that returns a DateTime. It performs a couple of quick table selects to get its return value. This function is already in use throughout the database - in default constraints, stored procs, etc. I would like to change the implementation of the function (to remove the table hits and make it more efficient) but ap...

ASP.Net Architecture Specific to Shared/Static functions

Hello All, Could someone please advise in the context of a ASP.Net application is a shared/static function common to all users? If for example you have a function Public shared function GetStockByID(StockID as Guid) as Stock Is that function common to all current users of your application? Or is the shared function only specific ...

Getting .NET schema for a stored procedure result

I have a couple of stored procedures in T-SQL where each stored procedure has a fixed schema for the result set. I need to map the result sets for each procedure to a POCO object and need the column name and type for each column in the result set. Is there a quick way of accessing the information? The best way I have found so far is ac...

Sql Server: chunking deletes still fills up transaction log; on fail all deletes are rolled back - why?

Here is my scenario: we have a database, let's call it Logging, with a table that holds records from Log4Net (via MSMQ). The db's recovery mode is set to Simple: we don't care about the transaction logs -- they can roll over. We have a job that uses data from sp_spaceused to determine if we've met a certain size threshold. If the thre...

SQL Query - Can I compare using LEN in SELECT clause?

I basically want to do this: SELECT HasComments = CASE (LEN(Comments) > 1) WHEN 1 THEN 1 ELSE 0 END FROM TableName In other words, return a boolean telling me whether the length of Comments is greater than 1. This gives me a syntax error. How can I accomplish this? ...

How to switch from VARCHAR to TEXT in SQL 2000?

What do I need to consider before I switch a bunch of fields from VARCHAR(bignumber) to TEXT? Aside from performance, and sometime in the far future TEXT will be deprecated, and aside from the fact that it looks like I need to drop and recreate the table to alter the column's data type? This is for SQL 2000-- I can't do VARCHAR(max) an...

Select Statement to show missing records (Easy Question)

I need some T-SQL that will show missing records. Here is some sample data: Emp 1 01/01/2010 02/01/2010 04/01/2010 06/01/2010 Emp 2 02/01/2010 04/01/2010 05/01/2010 etc... I need to know Emp 1 is missing 03/01/2010 05/01/2010 Emp 2 is missing 01/01/2010 03/01/2010 06/01/2010 The range to check will start with todays date and...

How to implement best matching logic in TSQL (SQL Server 2000)

I have two tables X and Y: Table X C1 C2 C3 1 A 13 2 B 16 3 C 8 Table Y C1 C2 C3 C4 1 A 2 N 2 A 8 N 3 A 12 N 4 A 5 N 5 B 7 N 6 B 16 N 7 B 9 N 8 B 5 N 9 C ...

I want to exchange the Value of a column in two different rows in Microsoft SQL server

Hi, I want to do the following two SQL Queries in Microsoft SQL SERVER UPDATE Partnerships SET sortOrder = 2 WHERE sortOrder = 1; UPDATE Partnerships SET sortOrder = 1 WHERE sortOrder = 2; The only problem is, I don't allow for sortOrder to contain the same value, it is a unique key. How could I get around this, because the first qu...