tsql

Nesting sql queries inside case

Hi guys, I am trying to write a SQL query where I am joining two tables and retrieving a few columns. Once this is done, based on a two fields (source table enum, id in the corresponding source table), I need to retrieve a value matching the id in the source table. The issue is that I cannot join all the source tables, and want to do ...

How to insert into sql server Many to many relationship tables

I imported data into csv table via .net app, pls help, how can i aInsert into all the tables so that i can query; student avg score to a question or test student answers to a question or answer frequency to a question etc My tables are in this form; Csv Ans Quest Test Ans_Quest_Test --------- ------ ...

How to deny delete on a table for all users

Within SQL Server 2005, is there a way, with a single statement, to deny delete on rows in a particular table for all users of the database? ...

SQL Server 2005/2008 Finding nth highest salary

For Finding nth highest salary I am using select salary from ( select distinct ROW_NUMBER() over (order by salary desc) as rownum,Salary from Employee )a where rownum=2 However if i have same salary like 70000 70000 60000 50000 50000 When executing the query i am getting second highest salary 70000 instead 60000 ...

TSQL/SQL 2005/2008 Return Row from one table that is not in othe table

I have to compare a row of one table that is not in another table TableA ID 1 2 3 4 NULL TableB ID 1 4 When comparing TableA with TableB ,the following o/p (NULL Can be ignored) ID STATUS 1 FOUND 2 NOT FOUND 3 NOT FOUND 4 FOUND I tried with SELECT case T.ID when isnull(T.ID,0)=0 then 'NOT FOUND' ...

Problem with the use of TOP 1 in a query

I wrote the following query to obtain a date, remove it's time part and add the time I wanted. If I run this query without the TOP clause, it works well. But when I add it, it returns the following exception: "Conversion failed when converting date and/or time from character string." Here is the query: SELECT TOP 1 CONVERT(DateTime, ...

TSQL Bulk Insert

Hi all, I have such csv file, fields delimiter is ,. My csv files are very big, and I need to import it to a SQL Server table. The process must be automated, and it is not one time job. So I use Bulk Insert to insert such csv files. But today I received a csvfile that has such row 1,12312312,HOME ,"House, Gregory",P,NULL,NULL,NULL,...

how i can use like statement in c#

i am adding parameter by qry = qry.Replace("{criteria}", "info.abc LIKE '%?val%'"); command not worked if i removed ' ' from the command then it give a error how i can search the table in c# ...

How do you join a query between 2 databases involving sys tables?

-- find last usage info, -- how far back this information goes depends on the -- server/database settings select -- ss.name as SchemaName, so.name as SprocName ,so.create_date as SprocCreated, so.modify_date as SprocModified ,so.object_id ,stat.last_user_seek,stat.last_user_scan,stat.last_user_lookup,stat.last_user_update ,stat.last_...

Select a string comparison as a boolean in tsql

In a tsql query, I want to have a calculated field that is the boolean result of a string comparison. It would look something like this: select name, (status = 'current') as IsValid from items But the query as I have listed is not valid. What is the correct syntax? ...

How does SQL Server determine the style for convert when it isn't specified?

Update: This is a bug but won't get fixed until the next release of SQL Server due to backward compatibility concerns. This is following on from this question which I answered but am still puzzled by. Adding TOP (1) to a query is sufficient to change the result from "Sep 3 2010" to "2010-09-03" (at least on my machine with British se...

What's your favorite SQL 2005 and SQL 2008 book?

Possible Duplicate: Best Book for a new Database Developer I'm looking to become an intermediate, eventually advance sql programmer. I'm currently half way between beginner and intermediate. I'd like to book to cover at least beginner to intermediate, or all the way even. Thanks Thank you all for your responses, I have chos...

Row_Number() and T-SQL View

I am trying to implement paging for a View in T-SQL : with TH_VW_UserFollowing as ( Select FollowerID, FollowingID, FollowingFullName, FollowingImage, FollowingUserName, dbo.GetUserFollowers(FollowingID) AS Followers, ROW_NUMBER() OVER (order by dbo.GetUserFollowers(FollowingID) DESC...

Is this SQL Query possible?

Suppose I have this data: Employee Task IsCurrent -------- ---- --------- Jeff 1 Yes Jeff 2 No Jane 1 No Jane 2 Yes Jane 3 No Jane 4 No Jake 1 Yes Employees have some number of tasks, and one of them will be current. The task number indicates an ordering -- t...

updating one table from another with complex conditions

Thank you for your help in advance. I am looking for a neat solution for the following update scenario: I have a table TableA with 5 columns (KeyCol, colA, ColB, ColC, ColD) I have another table Table2 with 2 columns (KeyCol, AvgCol) I want to do something like this: update AvgCol in table2, joining on KeyCol from this logic: if...

I'm wondering if somebody can help me improve my T-SQL query so that i can retrieve the SUMs of the money columns from an additional table... :)

First of all, thank you very much in advance for any attempt at helping me with this. My current query (below) works the way i want it to save the requirement mentioned in the title: I need to retrieve the SUM's of the money columns in my 'expenses' table AND each of these values must correspond to a period_ending value that is greater t...

Identity key on varchar column - T-SQL

Hi there. I want to know if I can create an Identity (auto increment on a Varchar column. and how can I make it a primary key and create foreign key references on other table. This is the code i have - CREATE TABLE Questions( QuestionID int IDENTITY PRIMARY KEY, QuestionNo as 'Q'+Cast(QuestionID as Varchar(10), Que...

RaiseError did not raise the error enough !?

Why the error does not appear at the UI layer? I am using ExecuteScaler BEGIN CATCH PRINT N'The transaction is in an uncommittable state. Rolling back transaction.' ROLLBACK TRANSACTION; DECLARE @ErrorMessage NVARCHAR(4000); DECLARE @ErrorSeverity INT; DECLARE @ErrorState INT; SELECT @ErrorMessage = ERROR_MESSAGE(), ...

Full Text-Search and Email notifications

Hi guys, I am going to implement search capabilities and Email messaging to my website. Any tips and tricks / Guidenance that I can use to implement these features ? I am using .NET. Thanks. ...

What are the pros and cons of the following database design?

I am looking to for suggestions and commments about the following design. All the tables are expected to be large tables (millions of records) and are updated and queried often and any kind of update is allowed (update, insert, delete). -- product create table Product( productID int not null identity(1,1), name varchar(100) not null, ...