Dynamic query based on user selection - sql

How can I generate a dynamic query based on user selection of data. I.E.
Table 1 has basic personnel data: name, social sec num... table 2 has address info: addr, city, state, zip
and table three has a work history of contracts that have been worked.
The user has choice of data to return:
Name, Address, Current contract, current name of place contracted, etc...
I have presented them with the list of options and I am storing in the database where the information is - tableName and fieldName ...
But I am not sure how to compile the data into a query dynamically. I get dynamic sql but this is a question of structure and retrieval.
Anyone done something like this or no a place to look?

If you need to feed this to a report all the time, I would suggest returning all the data to your report regardless, and then dynamically hiding the columns you don't need in the front-end application. That is generally easier.
If you absolutely have to do it from the SQL side, you could do something like:
SELECT
CASE WHEN #paramDisplayName = 1 THEN Name ELSE NULL END AS Name,
CASE WHEN #paramDisplayAddress = 1 THEN Address ELSE NULL END AS Address
...
...
...
FROM
Wherever
This would still return the column, though it would be empty.

Related

DB2 SQL - Set variable value based on user input

In DB2-LUW SQL, I can write a query to do this:
select * from customers
&input
When a user runs that they'll be prompted for input and can type:
where name = 'Bill'
And the query that actually gets executed will be:
select * from customers
where name = 'Bill'
I am trying to figure out a way to vary the value of &input to alter the query based on more basic user input - so they won't need to type in where name = 'Bill'.
For example, the user could be prompted to enter either YesBill or NoBill and depending on what they've entered, the value of &input will be set and the executed query would be either:
select * from customers
where name = 'Bill'
or
select * from customers
where name <> 'Bill'
The example is meaningless, I'm mostly wondering if it's possible to vary the value of &input without forcing the user to type in SQL code.
Hope that makes sense. Thanks for any help!
The answer to this question really depends on the SQL client that you are using.
For example, if you use DBeaver, then you can simply use : to prefix your variable names and then when executing the query, you will get a prompt to window to enter your variable names.
Other query tools may, nor may not, provide a similar capability

to search the data from database

I want to search the data from database from table. Throw one input box I give nput (like mobile, name, id, anything I want ) and search and give out put
Logic is something like that
SELECT * FROM table WHERE (condition)
For your case, you may as well use
SELECT 'mobile','name','id', '...any from its table...' FROM table_name

Microsoft Access 2013 sql return which table information came from

I am re-making a data base to store an inventory of ID numbers, Locations, and Item type. In my older data base there are a number of tables (16) or so. I have written code to produce a list of all items in the data base (around 1500) and produce the location of where they are.
Where i am having problems is I do not know how to make my data base produce which table each item came from. by this i mean i would like my code to produce the item tag(which it does), the items location(which it does), and which table the information came from(which it does not). I would like it to be a new column in the return.
thank you for any help. Here is an sample example of what my current code looks like. This is only one of the many unions i have
SELECT [Phones].[IT Tag], Phones.Location, from [Phones]
UNION ...
I would like something like this to work
//it tag and Locations are valid columns in my database table name should show phones for this select statement nd each statement after this should produce the correct table name.
SELECT [Phones].[IT Tag], Phones.Location,Phones.TableName from [Phones]
UNION ...
Put the table name into the individual queries:
select 'foo' as tablename from foo
union all
select 'bar' as tablename from bar
union all
etc...

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

Recordset returns the correct number of row but with all field empty

I have the same copy of access running in 3 cities right now. They work perfectly ok. They are 99% the same with one minor difference. Each of them has two views which use different odbc connection to different cities DB (all these databases are SQL Server 2005). The views act as datasource for some two very simple queries.
However, while I tried to make a new copy for a new city, I found that one of the simple internal query returns the correct number of row but all data are empty while the other query functions correctly.
I checked the data of these two views, the data is correct.
The one causing problem are like
Select * from View_Top where Name = "ABC"
when the recordset returns, even rs!Name give me an empty string.
Please help
Well the query looks a little wrong to me, try using ' instead of " to delimit your ABC string...
Without the definition of VIEW_TOP it's hard to tell where your error is, but if you're getting rows but the columns are NULL I'm guessing that VIEW_TOP (or something it depends on) includes an OUTER JOIN and you're pulling the columns from the wrong side of the JOIN.
SELECT
acc.FIRM,
acc.OFFICE,
acc.ACCOUNT,
a.CONV_CODE,
a.OTHER_AMT AS AMOUNT,
a.TRANS_DATE,
a.DESCRIPTN,
a.JRNAL_TYPE
FROM AccTrans AS a LEFT OUTER JOIN ACC AS acc ON a.ACCNT_CODE = acc.ACCNT_CODE
WHERE
(acc.SUN_DB = 'IF1') AND
(ANAL_T0 <> '') AND
(a.TRANS_DATE < '20091022') AND
(a.JRNAL_TYPE = 'MATCH');
This is the definition of the view. Indeed, in Access i am able to view the result of this query, it has data. that's why i know the recordset returns the correct number of row (by counting the loop in the code). sorry for my mistakes, i use Account in the where clause, the select statements should be like
select Firm, Office, Account, Trans_Date.... from
view_top
where account = 'ABC'
the query returns the right number of row but all row data (even the account field) are empty string.
then i found out what really cause the problem is the AMOUNT field, if i omit the amount, everything works. though i don't understand why.
view_top definition
"Name, Account, AccountCode, Amount, Date...."
Select Statements:
Select Name, Account, AccountCode, Amount, Date
From View_Top Where Name = 'xxx'
I found that if I omit the Amount, everything works.
Though I still don't understand why.