My question is pretty much like the question here:
How to find column names for all tables in all databases in SQL Server
But I am not able to comment due to reputation not being 50 or > and I can't put anything in the answer box because you're supposed to put ANSWERS in the answer box.
I got this code from Stack Overflow:
SELECT *
FROM master..sysdatabases
where dbid > 4
--order by dbid
order by name
and it works fantastic - it lists all of our databases.
What I would like to do is get a result table that shows these columns:
DatabaseName, TableSPViewName, FieldName, TypeSize, Indexed
DatabaseName would be JUST the name of the database.
TableSPViewName would contain
the name of all tables in that database
the name of any Stored Procs in that database
the name of all Views in that database
FieldName would be a listing of all fields in that table (if it were a table or view)
TypeSize would be the type and size of that field (like: int, varchar(##), bit ...)
EDIT SEPT 20, 2013 ---------------
ON THIS SITE:
http://blog.sqlauthority.com/2009/04/26/sql-server-list-all-the-tables-for-all-databases-using-system-tables/
I FOUND THIS:
exec sp_msforeachdb 'select "?" AS db, * from ?.sys.tables'
But that SP puts puts up a separate query window for each DB with all its tables so you get
DB with all its tables
DB with all its tables
DB with all its tables
DB with all its tables
I need:
DB 1 table 1 field 1
DB 1 table 1 field 2
DB 1 table 1 field 3
DB 1 table 2 field 1
DB 1 table 2 field 2
DB 1 table 2 field 3
DB 1 table 3 field 1
DB 1 table 3 field 2
DB 1 table 3 field 3
DB 2 table 1 field 1
DB 2 table 1 field 2
DB 2 table 1 field 3
DB 2 table 2 field 1
DB 2 table 2 field 2
DB 2 table 2 field 3
DB 2 table 3 field 1
DB 2 table 3 field 2
DB 2 table 3 field 3
EDIT #2 SEPT 20, 2013 ---------------
ON THIS SITE:
http://blog.clicdata.com/2012/08/02/how-to-list-all-tables-and-columns-with-types-in-a-sql-server-database/
I FOUND THIS:
SELECT tTables.name AS table_name,
tCols.name AS column_name,
tTypes.name,
tTypes.max_length,
tTypes.precision,
tTypes.scale
FROM sys.tables AS tTables
INNER JOIN sys.columns AS tCols
ON tTables.OBJECT_ID = tCols.OBJECT_ID
JOIN sys.types AS tTypes
ON tCols.user_type_id = tTypes.user_type_id
ORDER BY tTables.name;
But that just works for the CURRENT database in use.
It gets me REALLY CLOSE to what I'm looking for but I still need to have one query that does what the above does but for ALL DATABASES we have. See my long list of "DB 1 table 1 field 1" shown above. If we can get the above query to tack on the DATABASENAME onto the left side of what the above produces AND do it for all DBs we'll be in business!
One way to do this is
EXEC sp_msforeachdb ‘SELECT tTables.name AS table_name,
tCols.name AS column_name,
tTypes.name,
tTypes.max_length,
tTypes.precision,
tTypes.scale
FROM sys.tables AS tTables
INNER JOIN sys.columns AS tCols
ON tTables.OBJECT_ID = tCols.OBJECT_ID
JOIN sys.types AS tTypes
ON tCols.user_type_id = tTypes.user_type_id
ORDER BY tTables.name;’
For one complete table, insert results into temp table.
Related
Using the query wizard, I built two different queries doing similar functionalities that I am trying to combine into one query. I have two tables (same structure) that I am matching to find duplicates:
Query #1 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 7 to Table 2 Column 7
If all of those columns from Table 1 match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7 which will show the duplicates I am looking for).
Query #2 is as follows (Include ALL records from Table 1 and only those records from Table 2 where the joined fields are equal are applied to all the columns below):
Match Table 1 Column 3 to Table 2 Column 3
Match Table 1 Column 4 to Table 2 Column 4
Match Table 1 Column 5 to Table 2 Column 5
Match Table 1 Column 8 to Table 2 Column 8
My second query has the same 3 columns, except the last column is different.
If all of those columns from Table 1, match what’s in Table 2, it will identify the duplicates (I bring in Table 2 Column 7/8 which will show the duplicates I am looking for).
What I am trying to do:
Add an OR statement for the query to show both duplicates on matches for Columns 8 and Columns 7. Such as if Table 1 Column 7 matches Table 2 Column 7 OR Table 1 Column 8 matches Table 2 Column 8, show the duplicates.
Would this require a UNION query?
Here is the query for one of them:
SELECT TABLE1.COLUMN_3, TABLE1.COLUMN_4, TABLE1. COLUMN_7,
TABLE2.COLUMN_7, TABLE1.COLUMN_5
FROM TABLE1
LEFT JOIN TABLE2
ON (TABLE1.COLUMN_7 = TABLE2.COLUMN_7)
AND (TABLE1.COLUMN_3 = TABLE2.COLUMN_3)
AND (TABLE1.COLUMN_4 = TABLE2.COLUMN_4)
AND (TABLE1.COLUMN_5 = TABLE2.COLUMN_5);
The given SQL will not run because of spaces in table and column names.
This example eliminates those spaces.
SELECT
Table1.Column3 , Table2.Column3
,Table1.Column4 , Table2.Column4
,Table1.Column5 , Table2.Column5
,Table1.Column7 , Table2.Column7
,Table1.Column8 , Table2.Column8
From Table1
Left Join Table2
On
( Table1.Column3 = Table2.Column3
AND Table1.Column4 = Table2.Column4
AND Table1.Column5 = Table2.Column5
AND ( Table1.Column7 = Table2.Column7
OR Table1.Column8 = Table2.Column8
)
)
Create query 3; add Q1 and Q2 - join them on F7. This will result in all matches of that field.
Create query 4; add Q1 and Q2 - join them on F8. This will result in all matches of that field.
You now have 2 data sets with those matches (you call duplicates). Then the decision is how to present/display. If you need them in a single record set - write those into a single common temp table. But otherwise you can easily present them together as sub reports/forms.
i have a requirement like below
i have two tables in same data base, both table have same structure and column count.but the columns not present in the same position.
ex:
table 1
id name age
1 dhileep 22
2 uday 33
table 2
id age name
1 20 udayga
2 22 uday
i have id column is same for all tables, if i change the table also i have id same, but may columns name and column count and data count will change.
my final output is:
column_name id table1 table 2
name 1 dhileep udayga
note: i gave above as example, the count of columns is more than 500 and data exist approximately 50000+
use Sql JOIN .to join the 2 tables
use the following answer .i think it is useful for u.
SELECT t1.id,t1.name,t2.name FROM table1 AS t1 JOIN table2 AS t2 ON t1.id = t2.id
Just wondering will it be possible to insert records into a table from 2 difference sources in SQL?
Example:
Table 1
Number
1
2
Table 2
Name
Alex
Amy
I want to insert records into table 3 from table 1 and table 2 and the result for table 3 should be:
Number Name
1 Alex
2 Alex
1 Amy
2 Amy
Any way that I can do it in SQL Server?
Try a CROSS JOIN and a SELECT ... INTO:
This join relates every-with-every row. The result will be filled into a new table on the fly:
SELECT Nrs.Nr
,Nms.Name
INTO dbo.TheNewTable
FROM dbo.NumberTable AS Nrs
CROSS JOIN dbo.NameTable AS Nms;
See the result:
SELECT * FROM dbo.TheNewTable;
connect for two connections .
use a script not only in SQL :javca for example.
use hashmap and hashet ...
isnert in temporary table(drop on commit for example).
copy in table 3.
-don't forget to close connections.
I have a SQL Server database with two tables, table 1 lists membership records and table 2 lists the names for each membership record. There can be more than one person in table 2 per record in table 1.
table_1.MembershipNumber
table_1.MemberType
table_1.StartDate
table_2.MembershipNumber
table_2.FirstName
table_2.LastName
table_2.Age
I would like to create a view which filters out records in table 1 where the age of anyone in table 2 is over the age of, say, 50.
CREATE VIEW [dbo].[vw_Membership]
AS
Select t.MembershipNumber ,tt.FirstName,tt.LastName
from MembershipNumber_table_1 t
INNER JOIN MembershipNumber_table_2 tt
ON t.MembershipNumber = tt.MembershipNumber
AND MemberType = 'A'
WHERE (DATEDIFF(yy,MembershipNumber_table_2.Age,GetDate()) -
CASE WHEN((MONTH(MembershipNumber_table_2.Age)*100 + DAY(MembershipNumber_table_2.Age)) > (MONTH(GetDate())*100 + DAY(GetDate()))) THEN 1 ELSE 0 END)>50
GO
OR
WHERE DATEDIFF(YEAR, MembershipNumber_table_2.Age, GETDATE())>50
I am trying to create a SQL view which contains columns from different tables; the columns are different data types.
For example;
I have table a with a column that contains usernames. The data type of this column is nvarchar.
I then have table b, which has a column that contains whether a document was printed in colour or not – the data is either yes or no. The data type of this column is bit.
I want the view to show both the above columns side by side, so I can then pull the information into Excel for reporting purposes.
I am pretty new to SQL so I am learning as I go along.
Like PM77-1 said, you'll have to have some way to tie the two tables together. For example, if your table b also has the userID of the person who printed the document out, your tables would look like this:
Table A Table B
---------------------------- -----------------------------------
userID userName docID docName inColor userID
---------------------------- -----------------------------------
1 userName1 1 docName1 1 1
2 userName2 2 docName2 0 2
3 userName3 1 docName1 1 2
3 docName3 0 1
3 docName3 1 2
2 docName2 1 3
and your query could look like this:
SELECT a.userName, b.docName, b.inColor FROM a INNER JOIN b ON a.userID = b.userID ORDER BY a.userName, b.inColor;