Using variable name instead of a Table name in Select Statement - sql-server-2008

I'm working on SQL Server to get the datas from one database and compare with another database. The database we have, has around 400's of table. I have to write a query to get all the table names based on the DB name, and having one column name, I have to get the datas from all the tables.
Still now, I have written a query to get the primary key value and to get the table names having that primary key value. My plan is to call the primary key details in first cursor, and within that create another cursor and fetch the details of the table name and column name.
Based on the values retrieved, I have to write a query to fetch the datas like "select * from #cursor_variable_tablename where primarykeyval = #cursor_variable_primarykeyval".
Is it possible to work like this by calling a variable instead of giving the table name.?
Please help me with this. Thanks In Advance.

Not sure what you are trying to achieve here but you can use dynamic sql to execute the query having table name as a variable.

Related

Adding a new column in table once I've run an SQL select query

I've run the below SQL query in my phpMyAdmin interface to bring 3 columns together into one new column called media_location as below. Once I run the query I see the results exactly as expected but when I go back to my table I can't see the new column.
SELECT CONCAT(`rel_path`,`filename`,'.', `file_extension`) as media_location
FROM `ulv8_jreviews_media`
A SELECT statement fetches data from the database. It does not change the underlying table structure. What you are doing is concatenating the values in the output of your SELECT query. These changes are not persisted in the database.
To change the table structure you need to execute ALTER TABLE statement and then populate the new column with some data.
ALTER TABLE ulv8_jreviews_media
ADD media_location varchar(255);
UPDATE ulv8_jreviews_media SET media_location = CONCAT(`rel_path`,`filename`,'.', `file_extension`);

Compare a list of records to a table in database and list their timestamp

I've a list of records (in excel) which I want to lookup in SQL table to find their entry date in table.
For example I've name of 200 customers in an excel sheet which are also in my SQL table but there are many others as well. I want to compare those users in table and find their date of joining the table i.e the date they were added to the table.
Do you have any column such as "customer_id"? If not, create it in the tables as it will make it easier for you to select and join tables. You can use the alter table statement to add the new column. After adding the columns, join the tables and use the select statement to find the required record.
You must have a key to bind those data. Any other way can be dangerous linking these informations. Maybe, the way is adapt your application or excel to provide this bind between the data.

MySQL - Get Column Names & SELECT At Same Time?

I know of the following two ways to show column names of a table:
SHOW COLUMNS FROM tablename
and
DESCRIBE tablename
How can I do a SELECT FROM tablename and also return the columns (If possible)?
The reason I want to do this is because in Nim, the MySQL module doesn't provide any proc for returning an associative array of results that you can reference by column name. You have to get results like x[0]. I'd like to create my own module for this and I don't want to execute two different queries for it.

fist time SQL syntax. Concatenate some string to each instance of a field in a table

using mySQL for the first time for what I believe must be a really easy task.
I have a table called "incidents". In that table there is a field called "id". There are about 90 records in this table.
For each record, I'd like to concatenate:
'somestring'+id
What I've tried:
SELECT * FROM incident;
CONCAT('https://somewebsite.com/reports/view/',id);
After hitting go I'm then being told I have an error in my syntax?
How would I create a SQL statement that returns the concatenation of the same string with each unique ID in a table where the ID is the field to be appended?
SELECT CONCAT('https://somewebsite.com/reports/view/',id) FROM incident;

Select count from each table from a list stored on a table

I have a table tbls with a field name containing names of tables.
I'm trying to form a statement to get the number of rows of each of this tables.
Should I use a stored procedure or is there a simpler way to do it?
There is a thread that talks about how you can Get record counts for all tables in MySQL database. Using the result, you can constraint the result set to tables specified in the tbls table.