I have 2 tables
Table 1:
DB1, DB2, DB3, DB4, DB5, Some other identifiers
Table 2:
EnumText, EnumValue, Some other identifiers
What I want to do is to:-
Select data from Table 1
During Select, change the alias of columns DB1 to DB5 using the Data in Table 2's EnumText column where EnumValue will contain DB1 to DB5.
A sample command would be:
Select
DB1 as [Select EnumText from Table2 where EnumValue='DB1'],
DB2, DB3
from Table1
This command won't work but I hope you people got the idea.
Thank you in advance.
You can't have dynamic aliases without dynamic SQL. If you did change the resultset column names, how do you access them via name? (I wouldn't use index because it will break)
So, you can add a fixed column to describe it. And read that
Select
DB1, foo.EnumText AS DB1EnumText,
DB2,
DB3
from
Table1
CROSS JOIN
(Select EnumText from Table2 where EnumValue='DB1') foo
Related
Im trying to update all the values in 2 particular columns for two different tables we have to HEX so far without luck in our PostgreSQL 8.4.7 on Centos.
Here are the two columns types and the table name they belong too:
-node_id is a smallint column default 1. this belongs to table1
-value is a bytea column that belongs to table2
I can do a select on table2 for the value:bytea column with the following selec:
SELECT encode(value::bytea, 'hex') FROM table2;
But this syntax does not work for table1 node_id:smallint column.
I've try the following commands without luck in and effort to change the values of the columns to hex on the respective tables.
ALTER TABLE table2 SET value TO 'hex';
ALTER TABLE table2 MODIFY value TO 'hex';
UPDATE table1 (value::bytea, 'hex');
UPDATE tbl table2 SET value::bytea TO 'hex';
SET encode(value::bytea, 'hex') FROM table2;
There is any proper way to do this in a one command like the above?
Thanks in advance for any help.
I was able to solve my problem with the following command:
CREATE TABLE "name" AS select *, encode(value, 'hex') as "column_name" from "table";
I have a huge database in Sql-Server and I need to get all the names of the tables into one new table that I have made. This can be done?
I appreciate your help.
The new table has the fields ID, TableName, Status. Id is the identity and status for now will be 1, not null
Use this query below to get all tables name from your database
SELECT name FROM sys.tables
Then you can do a insert query like -
insert into newtable(name) select * from sys.tables
I'm trying to create a procedure that can create a table with not specific number of columns.
My query returns a value of 3 meaning it needs 3 columns (has to be dynamic).
I have create a #variable to set the name string of the tables but I don't know how to formulate the CREATE TABLE statement to actually create the table with the columns from this string.
Any kind of help would be appreciated guys.
You can get the columns on a table out of the sql database with
select
bb.name,
bb.colid
from sysobjects aa
inner join syscolumns bb
on aa.id = bb.id
where aa.name ='tblMyTable'
The name is the column name, the ID the number. You could select column names from the list and use dynamic sql to build a select. Not sure how you decide what columns you are after from the table.
in vb.net how would you loop through database1 to check that all records in database2 exist in Database1 and the other way areound if a record exist in database1 and doesnt exist in database2 then delete it from database1.
so database2 is my reference
how can i do this using queries, also does it have to include nested looping?
note that the records are not in the same order
Thanks
This query will return all of the rows in the attached table that are not in the local version of the table
SELECT * FROM attachedTable
WHERE col1 NOT IN( SELECT lt.col1 FROM localTable as lt)
And this will do the converse, returning all rows in the local table that are not matched in the remote table.
SELECT * FROM localTable
WHERE col1 NOT IN( SELECT rt.col1 FROM attachedTable As rt)
is there any way to add a relation between 2 tables in different databases ?
For example:
db1.dbo.table1 field id = db2.dbo.table2 field id
the nearest thing that i´m looking for is something like:
CREATE TRIGGER RELATIONAL on IDCLIENT
FOR INSERT
As
IF (SELECT COUNT(*) FROM table1) = 1
INSERT INTO db2.dbo.table2(ID)
SELECT ID FROM table1
You can create a synonym for the other table in another db, and then you can reference it by its synonym name.