sql

MySQL Lookup table and id/keys

Hoping someone can shed some light on this: Do lookup tables need their own ID? For example, say I have: Table users: user_id, username Table categories: category_id, category_name Table users_categories: user_id, category_id Would each row in "users_categories" need an additional ID field? What would the primary key of said table...

nested subreports affecting main report

I am creating a report using reporting services which formatted as follows: Main report (passing mainid to SP and subreport 1 and 2 )       |---display data from a stored procedure(SP1)       |---Subreport 1 (passing subreport id to SP and subreport 1.1 and 1.2)             |---display data from a stored procedure(SP10)             |---...

SQL to get list of dates as well as days before and after without duplicates

I need to display a list of dates, which I have in a table SELECT mydate AS MyDate, 1 AS DateType FROM myTable WHERE myTable.fkId = @MyFkId; Jan 1, 2010 - 1 Jan 2, 2010 - 1 Jan 10, 2010 - 1 No problem. However, I now need to display the date before and the date after as well with a different DateType. Dec 31, 2009 - 2...

Simple join gives unnecessary rows,How to get join properly with only matches in both tables

Table 1 Field1 Field2 AA 20 AA 20 AB 12 AC 13 Table2 field3 field4 AA 20 AA 20 AC 13 AD 23 AW 21 output required: newfield field2 field4 AA 20 20 AA 20 20 AC 13 13 I used: select field1 as newfield, t1.field2,t2.field4 from table1 t1 join table2 t2 on t1.field1=t2.field3 This does not give the required output,Ple...

How to Store and Retrieve Images Using SQL Server (Server Management Studio)

I am having difficulties when trying to insert files into a SQL Server database. I'll try to break this down as best as I can: What data type should I be using to store image files (jpeg/png/gif/etc)? Right now my table is using the image data type, but I am curious if varbinary would be a better option. How would I go about inserting ...

Is there a set based solution for this problem?

We have a table set up as follows: |ID|EmployeeID|Date |Category |Hours| |1 |1 |1/1/2010 |Vacation Earned|2.0 | |2 |2 |2/12/2010|Vacation Earned|3.0 | |3 |1 |2/4/2010 |Vacation Used |1.0 | |4 |2 |5/18/2010|Vacation Earned|2.0 | |5 |2 |7/23/2010|Vacation Used |4.0 | The business ...

Entity Framework doesn't like 0..1 to * relationships.

I have a database framework where I have two tables. The first table has a single column that is an identity and primary key. The second table contains two columns. One is a varchar primary key and the other is a nullable foreign key to the first table. When adding the tables to the model I get the following validation error: Condition...

SQl server 2008 permission and encryption

i have made columns in some of the tables encrypted in sql server 2008. Now as i am a db owner i have the access to encode and decode the data using the symmetric key and certificate. But some other users have only currently datareader and datawriter rights ,and when they execute any SP referring the logic which uses the key and certific...

SQL Server: compare columns in two tables.

Hi, I've recently done a migration from a really old version of some application to the current version and i faced some problems while migrating databases. I need a query that could help me to compare columns in two tables. I mean not the data in rows, I need to compare the columns itself to figure out, what changes in table structure ...

SQL Server - CAST AND DIVIDE

DECLARE @table table(XYZ VARCHAR(8) , id int) INSERT INTO @table SELECT '4000', 1 UNION ALL SELECT '3.123', 2 UNION ALL SELECT '7.0', 3 UNION ALL SELECT '80000', 4 UNION ALL SELECT NULL, 5 Query: SELECT CASE WHEN PATINDEX('^[0-9]{1,5}[\.][0-9]{1,3}$', XYZ) = 0 THEN XYZ WHEN PATINDEX('^[0-9]{1,8}$',XYZ) = 0 THEN CAS...

What's faster? select [object] or select new {[object].value1, [object].value2} with linq

I was hoping some of you guru's out there know the answer to this question. Which is faster? A or B A: var item = from d in database select d; B: var item = from d in database select new {d.item1, d.item2}; SEEMINGLY, it seems like selecting part of the table object would be faster than selecting the entire o...

Select statement with multiple 'where' fields using same value without duplicating text

I will start by saying that I don't think what I want can be done, but that said, I am hoping I am wrong and someone knows more than me. So here is your chance... Prove you are smarter than me :) I want to do a search against a SQLite table looking for any records that "are similar" without having to write out the query in long hand. T...

Revert scan state when prompting for variable values

How do you detect the current SCAN state and revert it after changing it? An exerpt from the script in question: SET SCAN OFF SET ECHO ON SET SQLBLANKLINES ON SET SCAN ON UPDATE TABLE_NAME SET CREATED_BY = &&created_by; SET SCAN OFF The problem is that if the script doesn't have the first line (SET SCAN OFF), then the code to prompt...

How can I create dates/times and compare them in SQL?

I'm trying to create a BBS using PHP and SQL, and I want to make it so the topic with the most current post is on top of the topic list. what is a easy way to check if one date and time is more current than another? ...

Change string in SQL Server to abbreviate

How do I return the everything in a string from a sql query before a certain character? My data looks like this: HD TV HM45VM - HDTV widescreen television set with 45" lcd I want to limit or truncate the string to include everything before the dash. So the final result would be "HD TV HM45VM" ...

SQL group and order

I have multiple users with multiple entries recording times they arrive at destinations Somehow, with my select query I would like to only show the most recent entries for each unique user name. Here is the code that doesn't work: SELECT * FROM $dbTable GROUP BY xNAME ORDER BY xDATETIME DESC This does the name grouping fine, but as...

Does clustered index on foreign key column increase join performance vs non-clustered ?

In many places it's recommended that clustered indexes are better utilized when used to select range of rows using BETWEEN statement. When I select joining by foreign key field in such a way that this clustered index is used, I guess, that clusterization should help too because range of rows is being selected even though they all have sa...

SELECT only a certain set of rows at a time

I need to select data from one table and insert it into another table. Currently the SQL looks something like this: INSERT INTO A (x, y, z) SELECT x, y, z FROM B b WHERE ... However, the SELECT is huge, resulting in over 2 millions rows and we think it is taking up too much memory. Informix, the db in this case, runs out o...

Case Statements versus coded if statements

What is more efficient - handling with case statements in sql or handling the same data using if statements in code. I'm asking because my colleague has a huge query that has many case statements. I advised her to take stress off of the DB by coding the case statements. I've found that it is more efficient...but why? ...

Help with a SQL query

How would I go about this? I have 3 rows like so: ID THREAD POST 1 1 Hello World 2 2 Hello galaxy 3 2 Hello people I need to return ID, Thread, and Post once for each thread only using the lowest ID. something like: SELECT * FROM table WHERE THRE...