sql

Iterate through "linked list" in one SQL query?

I have a table that looks basically like this: id | redirectid | data where the redirectid is an id to another row. Basically if a row is selected, and it has a redirectid, then the redirectid data should be used in it's place. There may be multiple redirects until redirectid is NULL. Essentially these redirects form a linked list ...

How should I pass a table name into a stored proc?

I just ran into a strange thing...there is some code on our site that is taking a giant SQL statement, modifying it in code by doing some search and replace based on some user values, and then passing it on to SQL Server as a query. I was thinking that this would be cleaner as a parameterized query to a stored proc, with the user valu...

SQL Alternative to performing an INNER JOIN on a single table

I have a large table (TokenFrequency) which has millions of rows in it. The TokenFrequency table that is structured like this: Table - TokenFrequency id - int, primary key source - int, foreign key token - char count - int My goal is to select all of the rows in which two sources have the same token in it. For example if my table ...

Truncate before SqlBulkCopy

I want to truncate a table before doing a SqlBulkCopy. Does SqlBulkCopy automatically truncate a table before copying the data? ...

Transform arbitrary SQL SELECT TOP(x) to a SELECT COUNT(*)?

I want to be able to take any arbitrary SELECT TOP(X) query that would normally return a large number of rows (without the X limit) and transform that query into a query that counts how many rows would have been returned without the TOP(X) (i.e. SELECT COUNT(*)). Remember I am asking about an arbitrary query with any number of joins, w...

How should I handle entries when the user is deleted?

I run a forum which I built myself. Ok so all users have the opportunity to delete themselves. But all their threads and posts will remain. But right now where it should say their username it's just blank. How should I handle this? Should I make a new user and call it e.g. "deleted user" and assign all threads/posts to that ID when t...

Get the inverse of a join?

I am using SQL Server 2005. I have three tables - Users, Groups, and GroupUsers. GroupUsers contains the two PKs for a many-to-many relationship. I have a view to get all the user information for a group as follows: SELECT * FROM GroupUsers JOIN Users ON GroupUsers.UserID = Users.UserId I want to create the inverse of this view - I w...

DB2 SQL - median with GROUP BY

Hi everybody, First of all, I am running on DB2 for i5/OS V5R4. I have ROW_NUMBER(), RANK() and common table expressions. I do not have TOP n PERCENT or LIMIT OFFSET. The actual data set I'm working with is hard to explain, so let's just say I have a weather history table where the columns are (city, temperature, timestamp). I want ...

SQL: Select maximum value for each unique key?

Sorry, I'm not sure how to phrase that and I'm really not very good with SQL. The db engine i SQL Server Compact. I currently have this query: SELECT * FROM Samples WHERE FunctionId NOT IN (SELECT CalleeId FROM Callers) ORDER BY ThreadId, HitCount DESC Which gives me: ThreadId Function HitCount 1 164 6945 ...

PHP/SQL help with function

Help my write a function. function reportThread() { global $id; $result = mysql_query("SELECT is_checked FROM reports WHERE url = '?threadID=$id'"); $r = mysql_fetch_assoc($result); if ($r['is_checked'] == 0) { echo "<script type=\"text/javascript\">alert(\"A moderator has been notified.\");</script>"; mysql_query("INSERT INTO repo...

looking for some cool Silverlight chart Demos

Hello everyone, I am currently using Reporting Service of SQL Server 2008, and I think the user interface is not very elegant. And I want to use Silverlight as user interface to render reporting service backend data. Two questions, Is it possible? Reporting Service exposes interface which Silverlight could retrieve data and render? I ...

SQL Server output parameter issue

I am using SQL Server 2008 Enterprise. I am learning OUTPUT parameter of SQL Server stored procedure. For example, stored procedure sp_add_jobschedule has an OUTPUT parameter called schedule_id. http://msdn.microsoft.com/en-us/library/ms366342.aspx My confusion is, looks like OUTPUT parameter could be provided an input value and also r...

how to see active SQL Server connections?

Hello everyone, I am using SQL Server 2008 Enterprise. I want to see any active SQL Server connections, and the related information of all the connections, like from which IP address, connect to which DB or something. Any existing tools to solve this issue? thanks in advance, George ...

how to see SQL Server cache hit rate?

Hello everyone, I am using SQL Server 2008 Enterprise. I will issue a lot of query to SQL Server. And I think SQL Server itself will use some optimization internally, like pre-fetching data into SQL Server internal data cache or accessing frequent requesting data from cache before load from physical page file to improve performance. An...

How can I get the almost last substring from "/" delimited string in T-SQL?

If I've got a string that consists of other strings delimited with "/" character (xxx...xxx/xxx/xxxx) how can I get the last and the almost last (the one before last) part with t-sql? It should probably be some combination of charindex() and right(). ...

SQL Server: View and DateTimes

I have the following table Foo: start | end | cause -------------+-------------+---------- 2007/12/22 | 2008/02/12 | 2 2008/03/18 | 1900/01/01 | -1 there are stored various periods of time. cause '-1' and end '1900/01/01' means the time period has no end. Under certain conditions, the application using thi...

Why doesn't this prepare statement work in MYSQLI?

I created this code: $statement = $db->prepare("SELECT * FROM phptech_contact"); $statement->execute(); $result = $statement->result_metadata(); $object = $result->fetch_object(); print_r( $object ); When I run it, it doesn't work. Can anybody tell me why it doesn't work? I have 20 rows in this table so data should be returned. ...

SQL Views and Sprocs

I've made it a company policy that all data access has to be done via Sprocs. That includes Selects, Updates, Deletes and Inserts. We're starting to become overrun by them. Am I overkilling it? The selects are being executed, and the results dumped into a custom DAL. I have a feeling that this is a holy war kind of question. I'm ...

Having a URL title to match a mysql query

I have SQL rows with this information: Example - Exampleish (Exampleishbosh) not all have () in them, so would it be a good idea to strip the - and () out of the url and then just do a search with the remaining words in the SQL query? http://www.co.com/example_exampleish_exampleishbosh/ or is this a bad idea? ...

SQL Server linked server performance

Hello everyone, I am using SQL Server 2008 Enterprise. And I am using Linked Server technologies to link another SQL Server 2008 Enterprise instance from another server. I write TSQL to manipulate objects (e.g. tables) from both server instances. My question is, for linked server, is there a big performance issue? If yes, what is the k...