sql

sub query with parameter?

Hi Pros, I'd like to ask help on this small query of mine. Im doing a query and a sub query on my sub query i want to have it parameterized. Is there a way to do it? Please see my query script. select sum(issue) as [Issue], sum(nonissue) as [NonIssue] from ( AS select case when isissue = 1 then 1 else 0 end as 'issue', ...

SQL min / max group by question

I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009 12:00 12/1/2009 12:12 1 12/1/2009 04:00 12/1/2009 04:20 2 12/2/2009 10:15 12/2/2009 10:22 2 12/2/2009 11:00 12/2/2009 11:01 ...

Finding a stored procedure

Hi , How can we find a particular stored procedure. I need to find a stored procedure which I don't know is in which database. Can somebody please, help with a script. Thanks ...

Can I get SQL injection attack from SELECT statement?

2 Questions actually: I know i must use Stored Procedures as much as Possible, but i would like to know the following please. A: Can i get a SQL Injection attack from a SELECT statement such as (Select * from MyTable) ? B: Also, can i get a SQL Injection attack when I use the SQLDataSource in ASP.NET? ...

How to sort by number in SQL Server?

I have a table with a column stored as string, but it is really a number like this: 17 - Doe 2 - Mike 3 - James I need to sort them and create a output like this: 2 - Mike 3 - James 17 - Doe How to write the SQL? Thanks in advance! ...

SQL Server: any equivalent of strpos()?

I'm dealing with an annoying database where one field contains what really should be stored two separate fields. So the column is stored something like "The first string~@~The second string", where "~@~" is the delimiter. (Again, I didn't design this, I'm just trying to fix it.) I want a query to move this into two columns, that would...

SQL Server BCP utility not updating all rows under Vista (Windows Server 2008)

Hi, We have a database table that we pre-populate with data as part of our deployment procedure. Since one of the columns is binary (it's a binary serialized object) we use BCP to copy the data into the table. So far this has worked very well, however, today we tried this technique on a Windows Server 2008 machine for the first time...

How do I use T-SQL Group By with multiple tables?

I have a table of suppliers, and tables Computers, Cameras, Displays all containing the SupplierID field. I am trying to do a T-SQL that will list all the suppliers, with a count of all rows. I can do them one at a time with: SELECT SupplierID, COUNT(dbo.Computers.ComputerID) as Computers FROM Supplier INNER JOIN Computers ON S...

Unpivot vs. Union queries in T-SQL 2008

Warning: This is a long question The database that I am pulling data from has the table structure like this Table: ClientSales ClientSalesId int identity (1, 1) (PK) ClientId int (FK) TermId int (FK) StudentType1Population int StudentType1Adjustment int StudentType1Sales int StudentType1SalesAdjustment int Student...

SQL MS SQL Server V.8 UNION problem

Hi, I am using this query: SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID WHERE (pa.AdvertiserID = 31331) AND (pat.LastAdDate > 7 / 1 / 2009) ORDER BY pat.PublicationID And it returns 0 results. What I am trying to d...

I'm trying to make a forum

So I have these tables: Topics id, title, date Posts id, title, text, date, user, topic_id How should I structure it so that the first post, the topics text, is on top of all other posts in the topic? Sort them by date? Or is there a smarter way? ...

Load SQLite database from remote SQL Server?

I'm using SQLite ADO.NET in my project so that I can unit test using an in-memory database. I want a copy of my real database but it is across the server. From what I've read, it looks like I have to specify ":memory" for the data source for the SQLite connection string. My problem is that I don't even know if it's possible to load a ...

UNION SQL statement not working

Hi, Why is this wrong and how can I put it right? SELECT PublicationID FROM (SELECT DISTINCT pat.PublicationID FROM dbo.PubAdvTransData AS pat INNER JOIN dbo.PubAdvertiser AS pa ON pat.AdvTransID = pa.AdvTransID WHERE (pa...

Northwind query exercises/examples

Thus far all I have done with SQL are simple select statements, but now I've also started learning about INNER JOINs, GROUP BY clauses and inner queries. I learned the concepts, but now I’d like to do some exercises using the Northwind database, so that such queries would become second nature. I tried to come up with good examples tha...

SQL Algorithm Trouble - for loop to add and filter results

I am struggling yet again with SQL with my search function. Basically I want a stored proc that splits up a string into words then for each word I want to check using Full Text Search if it is in any of 2 tables. If results are found in these tables it should add them to any results it found for the other words and return a set where t...

Need help regarding cascading save,delete of non collection entity

I have an entity A who has entity B. Class Entity A { public EntityB; } Class Entity B { public Entity A; } Entity B has one to one relationship with A. I am trying to use cascade save,delete when entity A is saved so that I don't have to manually save entity B. It shpould be done automatically. my mapping for entity B looks...

How do I paramaterise a T-SQL stored procedure that drops a table?

I'm after a simple stored procedure to drop tables. Here's my first attempt: CREATE PROC bsp_susf_DeleteTable (@TableName char) AS IF EXISTS (SELECT name FROM sysobjects WHERE name = @TableName) BEGIN DROP TABLE @TableName END When I parse this in MS Query Analyser I get the following error: Server: Msg 170, Level 15, State 1, Proced...

What is the purpose (or use case) for an outer join in SQL?

Is an outer join only used for analysis by the developer? I'm having trouble finding a use case for why you would want to include data in two or more tables that is unrelated or does not "match" your select criteria. ...

Regex to extract fields and data types from sql statement

I have this sql statement: CREATE TABLE [dbo].[User]( [UserId] [int] IDENTITY(1,1) NOT NULL, [FirstName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [MiddleName] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_A What i want is regex code which i can use to get all fields and data type. So will return something li...

sql distinct values

I have city database with country,state,city,postalcode,areacode. The city is not distinct ..for eg one city is repeated for multiple postalcode. I just want the list of distinct city and the first postal code and areacode... if is use distinct it would let me select distinct country state city ...i cannot get other details like areacode...