Hi i am new to sql and c# queries. I am using a select statement and puttin # parameters in using SQL.
string cmd = "select (FirstName, LastName, Address, City, PostalCode, Country, Username) values (#FirstName, #LastName, #Address, #City, #PostalCode, #Country, #Username) from [Customer];";
It doesnt like my sql can anyone tell me why? it says incorrect syntax but i can not spot where?
Thanks
Dom
The SELECT statement is used to select data from a database.
If you want to retrieve data you don' t have to add the values keyword:
SELECT column_name,column_name
FROM table_name;
With your query:
string cmd = "SELECT FirstName, LastName, Address, City, PostalCode,
Country, Username FROM Customer;";
In case you wanted to retrieve data with the parameters this is the syntax:
SELECT column_name,column_name
FROM table_name
WHERE column_name operator value;
With your query:
string cmd = "SELECT FirstName, LastName, Address, City, PostalCode,
Country, Username FROM Customer WHERE FirstName=#FirstName AND
LastName=#LastName AND Address=#Address AND City=#City AND
PostalCode=#PostalCode AND Country=#Country AND Username=#Username;";
Altough this is not really correct because you can just check for the id you set on the customer table.
string cmd = "SELECT FirstName, LastName, Address, City, PostalCode,
Country, Username FROM Customer WHERE *yourID*=*#yourID*;";
You can have a try here:
http://www.w3schools.com/sql/sql_select.asp
http://www.w3schools.com/sql/sql_where.asp
Look at this link, you should be using a SELECT INTO statement.
http://www.w3schools.com/sql/sql_select_into.asp
SELECT column_name(s)
INTO newtable [IN externaldb]
FROM table1;
Related
I have the following sample code as reference:
Create table wait_weekly as select XXX
.....;
Create table wait_weekly_prev as select
....
from
Wait_weekly
group by
cube(var1, var2);
This works fine.
However, if I put the table wait_weekly either in the 'With' clause or directly in a subquery, like
with wait_weekly as (select XXX)
Create table wait_weekly_prev as select
....
from
(select XXX...)
group by
cube(var1, var2);
`
it will give me the same error message and won't recognize a variable is an invalid identifier.. Any suggestion? thanks.
Here is the sample code, but the reality is that it works in this sample code. Just imagine it's not working, and the error message is that 'invalid identifier for one of the variables in table Test". I did remove the cube( lastname, sex) and replaced with nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex, and the codes work; but I do need cube( ..., ...).
Hope this is clear enough, thanks.
create table test (
Lastname VARCHAR2(12),
Sex VARCHAR2(12),
Age NUMBER);
insert all
into test (Lastname, Sex, Age) values ('Sun', 'M', 8)
into test (Lastname, Sex, Age) values ('Thomas','M',12)
into test (Lastname, Sex, Age) values ('Sun','F',13)
into test (Lastname, Sex, Age) values ('Thomas','F',15)
into test (Lastname, Sex, Age) values ('Berg','F',18)
SELECT 1 FROM DUAL;
Example queries:
with test_1 as
(select * from test where lastname <> 'Berg')
select
nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex,
sum(Age) as Age
from test_1
group by cube(Lastname, Sex);
select
nvl(Lastname,'All_lastname') as Lastname,
nvl(Sex,'All_sex') as Sex,
sum(Age) as Age
from (select * from test where lastname <> 'Berg')
group by cube(Lastname, Sex);
Is there a way to return a blank string for a field in SQL, even if the field has data?
SELECT NAME, AGE, DOB, GENDER, CITY
FROM TABLE
I want DOB, to always return blank ''
In Oracle, an empty VARCHAR2 is equivalent to a NULL value.
So you can just do:
SELECT NAME,
AGE,
NULL AS DOB,
GENDER,
CITY
FROM TABLE
If you want to give it a specific data type then you can use CAST:
SELECT NAME,
AGE,
CAST( NULL AS DATE ) AS DOB,
GENDER,
CITY
FROM TABLE
try this:
SELECT NAME, AGE, ' ' as DOB, GENDER, CITY
FROM TABLE
You could use NVL2:
SELECT NVL2(dob, '', NULL) --rest of cols
FROM TABLE;
Please note that '' is the same as NULL. So you have:
SELECT '' AS DOB -- rest of cols
FROM TABLE;
How to insert few columns from TableA to tableB together with some additional values.
Following is one way I tried and failed, but it shows clearly what I want to achive:
Insert into
TableA (UserID, FirstName, Lastname,EmailAddress,IsActive,IsOnline,IsLockedOut,Comment)
values
(Select distinct UserID, FirstName, LastName, EmailAddress from TableB,0,0,0,'Imported')
You cannot use values when you use select keyword, also you should include constant/static values in your select statement itself, try this
Insert into
TableA
(
UserID, FirstName, Lastname,
EmailAddress,IsActive,IsOnline,
IsLockedOut,Comment
)
Select distinct UserID, FirstName, LastName,
EmailAddress ,
0,0,0,'Imported'
FROM TableB
You need to include the hard code values along with the columns before the FROM part of your query. SO, change your query to this:
Select distinct UserID, FirstName, LastName, EmailAddress, 0, 0, 0, 'Imported'
from TableB
I have two tables in a SQLite DB, and both have the following fields:
idnumber, firstname, middlename, lastname, email, login
One table has all of these populated, the other doesn't have the idnumber, or middle name populated.
I'd LIKE to be able to do something like:
select idnumber, firstname, middlename, lastname, email, login
from users1,users2 group by login;
But I get an "ambiguous" error. Doing something like:
select idnumber, firstname, middlename, lastname, email, login from users1
union
select idnumber, firstname, middlename, lastname, email, login from users2;
LOOKS like it works, but I see duplicates. my understanding is that union shouldn't allow duplicates, but maybe they're not real duplicates since the second user table doesn't have all the fields populated (e.g. "20, bob, alan, smith, bob#bob.com, bob" is not the same as "NULL, bob, NULL, smith, bob#bob.com, bob").
Any ideas? What am I missing? All I want to do is dedupe based on "login".
Thanks!
As you say union will remove duplicate records (note that union all won't!). Two records are considered duplicates when all their column values match. In the example you considered in your question it is clear that NULL is not equal to 20 or 'alan' so those records won't be considered duplicates.
Edit:
[...] the only way I can think would be creating a new table [...]
That is not necessary. I think you can do the following:
select login, max(idnumber), max(firstname), max(middlename), max(lastname),
max(email) from (
select idnumber, firstname, middlename, lastname, email, login from users1
union
select idnumber, firstname, middlename, lastname, email, login from users2
) final
group by login
However, if you're sure that you only have different values on idnumber and middlename you can max only those fields and group by all the rest.
You could left join the incomplete table to the complete one via the login. Then programmatically manipulate the resulting set.
Someone else designed this table and I am not allowed to modify it so bear with me.
I am trying to insert multiple rows from one table into another. The table where I am inserting the rows has an ID but it does not auto-increment. I cannot figure out how to manually increment the id as I insert rows. The current code throws an error:
Error running query. Page15.CaseSerial is invalid in the select list
becauseit is not contained in either an aggregate function or the
GROUP BY clause.
I've tried adding a GROUP BY clause with no success.
Here's the code:
insert into page4 (serial, caseserial, linkserial, type, add1, add2, city, state, orgname, prefername, email, firstname, lastname, salutation, contactstatus, workphone, notes, cellphone, nametype, homephone, fax, zip, payments)
select id = max(serial), caseserial, linkserial, type, add1, add2, city, state,
orgname, prefername, email, firstname, lastname, salutation, contactstatus,
workphone, notes, cellphone, nametype, homephone, fax, zip, payments
from page16
It would be nice if I could write something to get the highest id from page4 and insert the next highest.
Thanks!
declare #maxId int
select #maxId = max(yourIdColumn)
from YourTable
set #maxId = #maxId + 1
insert into YourTable (yourIdColumn, ....)
values (#maxId, ....)
Disclaimer: not sure how this would transpose over to other RDBMS's, but this is with SQL Server in mind. Also, this handles inserting only one value. If you need to insert a set of values, then please let me know.