sql

[MySQL] JOIN; only one record please!

OK, I have a complicated query from a poorly designed DB... In one query I need to get a count from one database, information from another with a link from another, here goes: Each blog has a type (news, report etc) and a section Id for a certain part of the site but it also can be linked to multiple computer games and sections) type (...

How do i retrieve a value output from a stored procedure using: OUTPUT INSERTED.BrandDrugDetailsID

I have a storeprocedure: CREATE PROCEDURE [dbo].[BrandDrugDetailsInsert] @BrandDrugDetailsID uniqueidentifier OUTPUT, @BrandDrugID uniqueidentifier AS BEGIN INSERT INTO Pharmacy_BrandDrugDetails(BrandDrugID) OUTPUT INSERTED.BrandDrugDetailsID VALUES (@BrandDrugID) END Anytime i try to retieve the value "@BrandDrugDetai...

Update an SQL table with its own rows with PostgreSQL

I want to try to update a postgresql table with it's own values. This table contains an overview of the products sold by year and month. CREATE TABLE sales ( sector character(3), brand character(4), product character(16), syear integer, smonth integer, units_sold integer, units_sold_year integer, CONSTRAINT pk_sales_id ...

Execute dynamic sql and pl/sql in Java via web interface.

Hi. Currently I'm making sort of SQL command line interface for web-based application. It should act roughly like sqlPlus. I have encountered a problem how to execute sql's. They can be both as SQL and/or PL/SQL. First way I thought that I can split them (by ';' or ';/') and detect separately if it's sql select or delete/update/insert ...

PL\SQL DML instruction

Is Commit a DML instruction in PL\SQL? ...

To count or not to count, that is the question

I have this table: Strawberries 2 Strawberries 3 Strawberries 4 Chocolate 3 Chocolate 4 Chocolate 5 Chocolate 6 Apples 3 Apples 4 Apples 5 Apples 6 My idea is to get the number of items, and the total of items per thing, like so: Item Number Total Strawberries 2 9 Strawberries 3 9 Strawberries 4...

Simple SQL query to LINQ

I have the following query: SELECT FROM tblMailToSend WHERE (DateToSend < @dateToSend OR DateToSend IS NULL) AND DateSent IS NULL @dateToSend is passed in as a param I'm trying to convert this to linq to sql query. I've got: db.MailToSend.Where(m => m.DateToSend == null || m.DateToSend <= dateToSend) .Where(m =>...

Rails: setting column alias attribute with find_by_sql

When I use a column alias in a query with find_by_sql, it doesn't appear to be set in the result objects, even when I add an attr_accessor for the property. class Country < ActiveRecord::Base attr_accessor :average_score def self.sorted_by_average_score sql = "SELECT country_id, AVG(score) AS average_score, countries.name " + ...

How to get a dataset value

New to VB.Net, How to insert or select the dataset value. cmd = New SqlCommand("Select * from table1", con) ada = New SqlDataAdapter(cmd) ds = New DataSet ada.Fill(ds) cmd = New SqlCommand("Select * from '" & ds.Tables(0) & "' ", con) mydatatable1.Load(dr3) It was showing the error in '" & ds.Tables(0) & "',...

get value from updated row

I'm trying to get the new rating from an UPDATE statement in java int userID = 99; String sql = "UPDATE table SET rating=rating+1 WHERE user_REF="+userID; statement.executeUpdate(sql); I can just do another SELECT statement, but isn't there a better way to retrieve the value or row while updating? ...

Rounding money to a decimal in SQL Server

I am trying to round to 3 decimal places from two variables of type money in SQL Server 2008. DECLARE @Val1 money = 554.5344 DECLARE @Val2 money = 84020.37 DECLARE @Result decimal(6,5) = @Val1/@Val2 *100 SELECT @Result The result above is 0.65. With a calculator it is 0.65999995001212, I need the result to be 0.659. ...

What is causing these SQL casting errors on my ASP.NET MVC/AJAX site? [Video showing problem]

I'm frustrated... my site has suddenly become very unstable. So much so that hitting refresh over and over will cause it to crash. To investigate, I turned off all error handling so I could see some YSOD's. Instead of trying to write it all out, I made a video showing the issue. You can see it here on YouTube. Here's a copy of the stac...

Help with a SQL syntax error message

switch($_GET["action"]) { case "add_item": { AddItem($_GET["idc"], $_GET["qty"]); ShowCart(); break; } case "update_item": { UpdateItem($_GET["idc"], $_GET["qty"]); ShowCart(); break; } case "remove_item": { RemoveItem($_GET["idc"], $_GET["id"]); ShowCart(); break; } default: { ShowCart(); } } function AddItem($itemId, $qty){ $result...

Should pre-commit tests use a big data set and fail if queries take too long, or use a small test database?

I am developing some Python modules that use a mysql database to insert some data and produce various types of report. I'm doing test driven development and so far I run: some CREATE / UPDATE / DELETE tests against a temporary database that is thrown away at the end of each test case, and some report generation tests doing exclusively...

differentiate rows in a union table

Hi I m selecting data from two different tables with no matching columns using this sql query select * from (SELECT s.shout_id, s.user_id, s.time FROM shouts s union all select v.post_id, v.sender_user_id, v.time from void_post v) as derived_table order by time desc; Now is there any other way or with this sql statement ...

How do I get the nth row in a SQL Server table?

how would you get the 5 value from a row of data. ...

IF EXISTS before INSERT, UPDATE, DELETE for optimization

There is quite often situation when you need to execute INSERT, UPDATE or DELETE statement based on some condition. And my question is whether the affect on the performance of the query add IF EXISTS before the command. Example IF EXISTS(SELECT 1 FROM Contacs WHERE [Type] = 1) UPDATE Contacs SET [Deleted] = 1 WHERE [Type] = 1 Wha...

test ssis package configurations - connection managers

What would be the best method to use for testing that connection managers are correctly pulling their connection strings from the package configurations on multiple packages (In VS 2008). In the past I have created a data flow which ran the query select @@servername and outputed to a text file. While this works, it seems a bit redundan...

Why is my index not used for this SELECT DISTINCT query on a text column?

I expected either index to be used for my SELECT DISTINCT query below: CREATE TABLE test( value TEXT ); INSERT INTO test (value) VALUES ('a'); INSERT INTO test (value) VALUES ('b'); INSERT INTO test (value) VALUES ('c'); CREATE INDEX value_i ON test(value(32)); CREATE FULLTEXT INDEX value_i_ft ON test(value); SELECT DISTINCT value ...

Restricting results of a many to many joined query

I have three tables like this (simplified version): Object --------------- ID | ObjectName --------------- Category ----------------- ID | CategoryCode ----------------- ObjectCategory --------------------- ObjectID | CategoryID --------------------- How can I select ObjectName and CategoryCode for all objects who don't belong to ce...