Simple sql query problem - sql

Hi I have this query in ms access which is somehow not working. All I need to do is to pull out donator name from donator table with the id of volunteer. But I need to get user input, volunteer name and pull out the related volunteer first. Please help.
SELECT volunteer.id, volunteer.name, donator.* FROM volunteer, donator WHERE Volunteer.id = Donator.vid AND Volunteer.name = Forms!frm5!Combo2;

Check your case on Database names. For instance, you reference volunteer.name and Volunteer.id. In some DB's, fields are case sensitive.
Also, you'd want to quote your volunteer name field.

Related

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Email address as a Table in SQL

I'm just wondering if it is OK to used the email address as a Table in SQL script? for example,
abcd_activity#cdf.com up
select distinct usr.user_name as USER_NAME, usr.id as USER_ID
from abcd#cdf.com usr, abcd_activity#cdf.com -- <-----------------------
where usr.id = ua.id
group by usr.name, usr.id;
My problem is that, I received the log file containing the error of the script and I noticed that the table used is email address. So, my assumption is that the caused of the error is in the table itself. Or, is it OK to use an email address as a lookup table in script?
This is a bit long for a comment.
No, it is not all right to have a table name like abcd_activity#cdf.com, for the simple reason that . and # are not allowed as table names. So, any attempt to do:
from abcd_activity#cdf.com
is going to result in a syntax error.
SQL does allow you to escape table names. You can get around this problem with one of these:
from "abcd_activity#cdf.com"
from `abcd_activity#cdf.com`
from [abcd_activity#cdf.com]
(which depends on your database).
More importantly, though, is that there is no sensible reason (that I can think of) to have a table represent separate emails. Normally, email would be a column in a table, not a table name.
So, you would have tables like:
Users Table
userId userName email . . .
And UsersActivity table:
userActivityId userId . . . .
email would be a column in Users, which you would look up using a JOIN.

Sql loop through the values on a table

first off, noob alert! :))
I need to construct a query that runs on many tables. The tables vary on name just on the last digits as per client code. The thing is, the values that change aren't sequential so looping as in i=1,2,3,... does not work. A possible solution would be to have those values on a given field on an other table.
Here is the code for the first two clients 015 and 061. The leading zero(s) must are essential.
SELECT LnMov2017015.CConta, RsMov2017015.DR, RsMov2017015.NInt, "015" AS CodCli
FROM LnMov2017015 INNER JOIN RsMov2017015 ON LnMov2017015.NReg = RsMov2017015.NReg
WHERE (((LnMov2017015.CConta)="6" And (LnMov2017015.CConta)="7") AND ((RsMov2017015.DR)=9999))
UNION SELECT LnMov2017061.CConta, RsMov2017061.DR, RsMov2017061.NInt, "061" AS CodCli
FROM LnMov2017061 INNER JOIN RsMov2017061 ON LnMov2017061.NReg = RsMov2017061.NReg
WHERE (((LnMov2017061.CConta)="6" And (LnMov2017061.CConta)="7") AND ((RsMov2017061.DR)=9999))
...
So for the first SELECT the table Name is LnMov2017015, the ending 015 being the value, the client code, that changes from table to table e.g. in the second SELECT the table name is LnMov2017061 (061) being what distinguishes the table.
For each client code there are two tables e.g. LnMov2017015 and RsMov2017015 (LnMov2017061 and RsMov2017061 for the second set client shown).
Is there a way that I can build the SQL, based upon the example SQL above?
Does anyone have an idea for a solution? :)
Apparently it is possible to build a query object to read data in another db without establishing table link. Just tested and to my surprise it works. Example:
SELECT * FROM [SoilsAgg] IN "C:\Users\Owner\June\DOT\Lab\Editing\ConstructionData.accdb";
I was already using this structure in VBA to execute DELETE and UPDATE action statements.
Solution found :)
Thank you all for your input.
Instead of linking 100 tables (password protected), I'll access them with SLQ
FROM Table2 IN '' ';database=C:\db\db2.mdb;PWD=mypwd'
And merge them all with a query, before any other thing!

How do I use case statement to aid altering another field?

I have a hopefully pretty simple question here. I'm converting some Access SQL script into Server Management Studio 2008.
Currently the Access script shows the following line of code:
IIf([rsosourceID]) IN (254,360,446),"MoneySavingExpert" as SourceName
Basically it's creating a temporary table with four columns, if the fields match say those 3 numbers on the left then it will populate a fourth column with their new name. (To be used for grouping in a later report)
How can I do something simillar in SQL? I've tried using a Case statement but to my knowledge that will only alter the field you're looking against.
In this case I need to look at one field then using it's results alter another, any ideas?
A case statement can return a new column using the value of any other column(s):
SELECT rsoSourceID,
rsoDescription,
rsoCategory,
case when rsoSourceID in (254,360,446)
then 'MoneySavingExpert'
else null end as SourceName
FROM TableName

sql query to select a value from one database

Hai,
I have two database and I want to select one value from one of the databases....
for that I want to pass one value and if that value is stored in the database I want to pick the id representing the value in the database.
that means the operation is that.....
first I select a row of data from one database by using a user control...
in that row there is a value (example "apple") and I want pass this value("apple") to the second database... in the second database the value("apple") having a id (example "australian") I want that the query search for that id("australian") and show that in the text box.
Please help me....
thanks to all advance....
example
first database
id name details
1 apple sweet
2 orange sweet
second database
id name details
Australian apple sold
Indian banana sold
Imagine that there are the two databases....
using a user control I select first row from first database and I want to pass that value apple to second database and find out the id australian from the second database and show that in a text box....
thank you.........
You can do a join between the two databases as long as you use the fully qualified prefix for each one.
I think you should go for the join , your query should look something like this
SELECT SecondDataBase.TableName.Id
FROM FirstDatabase.TableName INNER JOIN
SecondDataBase.TableName ON FirstDatabase.TableName.["Column contains Apple"] = SecondDataBase.TableName.["Column contains Apple"]
Fully qualified table name, which includes server name, db name, schema and table (e.g. MySqlServerInstance1.mydb1.dbo.table1) name will definitely work as long as one database server has a registered reference within a calling DB server. See this for things you have to do if you are using MS SQL Server: http://msdn.microsoft.com/en-us/library/ms188231.aspx
In MSSQL you normally reference a table using SchemaName.TableName
dbo.Fruit
The database is automatically determined by your connection string. FirstDatabase
So when you use dbo.fruit, the server automatically appends the database name to the table like
FirstDatabase.dbo.Fruit
If the user account has permission, you can select from a completely different database by specifying the database
SecondDatabase.dbo.FruitSales
To take it even further you can select from an entirely different SQL server if you have set up a linked server by specifying the linked server name like
SecondServer.ThirdDatabase.dbo.FruitShipping
So you can join between a table in your database and a table in your second database like
SELECT *
FROM FirstDatabase.dbo.Fruit AS F INNER JOIN
SecondDatabase.dbo.FruitSales AS FS ON F.Something = FS.Something
But you can even join between a table in your database and a table on a different server like
SELECT *
FROM FirstDatabase.dbo.Fruit AS F INNER JOIN
SecondServer.ThirdDatabase.dbo.FruitShipping AS FS ON F.Something = FS.Something