i am trying to insert a statement contains WHERE from two different tables :
the table i want to insert into is dbo.order
the other two tables are :
dbo.users. user_id.
dbo.packages. package_id.
another order field "notes".
the statement i tried is
insert into dbo.order
(customer_id,package_id,notes)
Select user_id,Package_ID
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner','notes value here';
any suggestions ?
There's no obvious join here so you'll get the cartesian product of Bader orders and beginner packages. Not sure what the notes value should be. If its a literal you can just include it in the select clause.
insert into dbo.order
(customer_id,package_id,notes)
Select
user_id,Package_ID , 'notes value here'
from
dbo.users,dbo.packages
where
username = 'bader' AND package_name = 'beginner';
insert into dbo.order
(customer_id,package_id,notes)
Select user_id, Package_ID, 'notes value here'
from
dbo.users, dbo.packages
where
username = 'bader' AND package_name = 'beginner';
Related
I use Npqsql 5. Is there any way to retrieve values for many different arguments using a single select?
For example I want to run:
"select value from table where id=#id1"
"select value from table where id=#id4"
"select value from table where id=#id12"
but as a single query in order to optimize.
Couple of options that come to mind:
select value from table where id = #id1 or id = #id4 or id = #id12
or
select value from table where id in (#id1, #id4, #id12)
I am trying to use a internal SQL command to query a table and use the result to query another but am getting the following problem
"Msg 137, Level 16, State 1, Line 10
Must declare the scalar variable "#ListofPropIDs"."
Any help would be appreciated
DECLARE #ListofPropIDs TABLE(IDs VARCHAR(100));
INSERT INTO #ListofPropIDs
SELECT Id
FROM Property
WHERE CreatedBy = 'oliver#test.co.uk'
SELECT
COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits = #ListofPropIDs;
GO
DECLARE #ListofPropIDs TABLE(IDs VARCHAR(100));
INSERT INTO #ListofPropIDs
SELECT Id
FROM Property
WHERE CreatedBy = 'oliver#test.co.uk'
SELECT
COUNT (Digits)
FROM dbo.CallInfo C
INNER JOIN #ListofPropIDs s
on s.IDs = c.Digits
without knowing what you want to achieve, something like this should work but may not be correct:
DECLARE #ListofPropIDs TABLE(IDs VARCHAR(100));
INSERT INTO #ListofPropIDs
SELECT Id
FROM Property
WHERE CreatedBy = 'oliver#test.co.uk'
SELECT
COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits in (SELECT IDs FROM #ListofPropIDs)
Your original query is trying to use a table variable as part of a where clause as if it where a column which is not correct.
#ListofPropIDs is a table and you're using it like a Value.
use :
SELECT
COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits IN ( SELECT IDs from #ListofPropIDs)
GO
You can't use a table as where condition value
Instead you can use SQL: IN Condition
SELECT COUNT (Digits)
FROM dbo.CallInfo
WHERE Digits IN (Select Id From #ListofPropIDs)
Or use Inner Join
SELECT COUNT (Digits)
FROM dbo.CallInfo C
Inner Join #ListofPropIDs T On T.Id=C.Digits
I have a table name Students having (studentId, StudentName,Address,PhoneNo)
i have provided a filter for user to select only StudentId from Combobox to get the details of Students... and generate Report.
I have written following Query to get student Detail :
(select * from Students where StudentId = stdId)
Here stdId is a Parameter that i pass from code
It works fine if i select single studentId.... But in user selection Comobobox i have also provided "ALL" if user Select All from combobox i want to display details of all student
So what should I pass in stdId if user selects All ?
I used inline Query in C# (not using SQL Stored Procedure)
You can do it like this .
SELECT * from Students s
WHERE s.studentId = ISNULL(#StudentId,s.studentId)
When "All" is selected in combo box pass null in your #studentid parameter. If you can not pass null in parameter then pass -1 or anything which can not be contain in you combox option and do it like this:(if -1= All)
SELECT * from Students s
WHERE s.studentId = #StudentId or #StudentId=-1
You can also try the answer given by Curt.
If user selects all, pass NULL to #StudentId and change your query to:
select *
from Students
where (StudentId=#StudentId OR #StudentId IS NULL)
In your stored procedure header, change the definition of #StudentId so that it can be passed as NULL.
ALTER PROCEDURE dbo.WhateverYourProcedureIsCalled( #StudentId int = null )
Then change your WHERE clause as follows
...
SELECT * from Students
WHERE #StudentId IS NULL OR StudentId = #StudentId
From your code, if ALL is passed, you can omit the part where you set the value of the #StudentId parameter. SQL Server will use the default if not passed.
If the where clause is included in the query you also need to supply a valid parameter value and it is therefore not possible to get all students when the where clause is included.
You need to differentiate your query based on the value selected in the combobox. You can do something like the following.
int studentId = 0;
//Where selectedValue comes from your combobox
Int32.TryParse(selectedValue, out studentId);
var query = "select * from Students";
if (studentId > 0)
{
query = query + " where StudentId = #StudentId";
//Remember to add studentId as parameter value
}
i have two tables first one name is "sales" and second one name is "items"
in both tables have same columns "code" and " qtd ";
i want write MYSQL query witch i need sum(qtd) from both of tables where code is same in both of tables .
for single table i am using this
"select code,sum(qtd) from sales group by code";
Try this:
Select code, sum(qtd)
from (
select code, qtd from sales
union all
select code, qtd from items) as innerTable
group by code
I have a products table and each product have a category field (this is not up to me to change and I need to read the data from it as it is), from the product table I wanted to update a category table with unique categories only (I also use a IFNULL to deal with empty categories), this is what I have so far that works:
string query = "INSERT INTO categories (name)
SELECT DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name)";
With the above query I am able to update the categories with unique category fields and every time I need to run it, it will add only categories that does not exist to the table.
But besides the above I also need to include a profile id with each category inserted something like this, which does not work:
string query = "INSERT INTO categories (profile_id, name)
SELECT " + profileId.ToString() + ",
DISTINCT(IFNULL(category, \"Uncategorized\")) AS category_name
FROM products
WHERE NOT EXISTS (
SELECT name FROM categories
WHERE name = category_name
AND profile_id = #profileId)";
Is there a way I could acomplish this with a query ?
The above gives me a message:
SQLite error near "DISTINCT": syntax error
SqliteMan error:
Query Error: near "DISTINCT": syntax error Unable to execute statement
Let me know if I havent been clear and what should I be more clear about.
You are using the DISTINCT keyword incorrectly: it is not a function. Try instead:
"INSERT INTO categories (profile_id, name)
SELECT DISTINCT '"+profileId.ToString()+"', IFNULL(category, 'Uncategorized')
FROM products
WHERE category_name NOT IN (
SELECT name FROM categories WHERE profile_id = "'+profileId.ToString()+"'
)
"
One assumes that profileId.ToString() is guaranteed to be safe from SQL injection, or else you would be escaping it/passing it as a parameter to a prepared statement?