Search Column name in Linked Server (Oracle) from Microsoft SQL Server [duplicate] - sql-server

This question already has answers here:
Get list of all tables in Oracle?
(24 answers)
Closed 3 years ago.
I need to retrieve Table Names from Linked Oracle Database which is on Linked Server with Microsoft SQL Server.
e.g. I can get table names in SQL Server using:
SELECT TABLE_NAME, COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%TELEPHONE%'
but, I need Oracle Code.
I am using Microsoft SQL Server with a linked server to Oracle.

SELECT
*
FROM
OPENQUERY(ORACLE_DB_NAME,'
SELECT
table_name,
column_name
FROM
cols
WHERE
column_name LIKE ''%TELEPHONE%'''
)
Based on your comment "Hi, SQL Query to retrieve Oracle Table Names", you can do the following:
SELECT
table_name,
column_name
FROM
cols
WHERE
column_name LIKE '%TELEPHONE%';

You can use USER_TAB_COLS or ALL_TAB_COLS view for it. Refer oracle documentation for more information on this views
SELECT TABLE_NAME, COLUMN_NAME
FROM USER_TAB_COLS
WHERE UPPER(COLUMN_NAME) LIKE '%TELEPHONE%';
UPPER is used in WHERE clause as names are case sensitive if they are created with double quotes and case insesitive if created with no quotes.
Cheers!!

Related

Is there any way or tool in SQL Server to find a column in the entire database based on the web page field name?

We have a web base tool where its database is SQL Server and we don't have an idea what could be the name of the column in database wrt field name on the web page.
We don't have access to the code. Is there any possible way to find the names in database column names?
You can use ;
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
view and you can look at the INFORMATION_SCHEMA.COLUMNS article about this view.
Following code can help in finding the column name along with the table name in the database:
use <replace with DB_Name>
GO
select * from information_schema.COLUMNS where COLUMN_NAME like '%<replace with column_name>%'
/*Also remove the angular brackets<> as well while replacing the column_name and DB_Name */
The INFORMATION_SCHEMA.COLUMNS view allows you to get information about all columns for all tables and views within a database :
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
To query for just one table you can use a query like this:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTable'

List of available keywords for SQL Server

It seems I can't use SHOW command in SQL Server like SHOW COLUMNS FROM... or SHOW TABLES.
Is there somewhere a list of all the commands that are available so I know what I can use ?
To get Table and Column information
Select * From INFORMATION_SCHEMA.COLUMNS
Select * From INFORMATION_SCHEMA.Tables
Reserved Words
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql
Stripped and modified from a validation routine
Declare #Reserved table (Word varchar(100))
Insert Into #Reserved values
('ADD'),('ALL'),('ALTER'),('AND'),('ANY'),('AS'),('ASC'),('AUTHORIZATION'),('BACKUP'),('BEGIN'),('BETWEEN'),('BREAK'),('BROWSE'),('BULK'),('BY'),
('CASCADE'),('CASE'),('CHECK'),('CHECKPOINT'),('CLOSE'),('CLUSTERED'),('COALESCE'),('COLLATE'),('COLUMN'),('COMMIT'),('COMPUTE'),('CONSTRAINT'),
('CONTAINS'),('CONTAINSTABLE'),('CONTINUE'),('CONVERT'),('CREATE'),('CROSS'),('CURRENT'),('CURRENT_DATE'),('CURRENT_TIME'),('CURRENT_TIMESTAMP'),
('CURRENT_USER'),('CURSOR'),('DATABASE'),('DBCC'),('DEALLOCATE'),('DECLARE'),('DEFAULT'),('DELETE'),('DENY'),('DESC'),('DISK'),('DISTINCT'),
('DISTRIBUTED'),('DOUBLE'),('DROP'),('DUMP'),('ELSE'),('END'),('ERRLVL'),('ESCAPE'),('EXCEPT'),('EXEC'),('EXECUTE'),('EXISTS'),('EXIT'),('EXTERNAL'),
('FETCH'),('FILE'),('FILLFACTOR'),('FOR'),('FOREIGN'),('FREETEXT'),('FREETEXTTABLE'),('FROM'),('FULL'),('FUNCTION'),('GOTO'),('GRANT'),('GROUP'),
('HAVING'),('HOLDLOCK'),('IDENTITY'),('IDENTITY_INSERT'),('IDENTITYCOL'),('IF'),('IN'),('INDEX'),('INNER'),('INSERT'),('INTERSECT'),('INTO'),('IS'),
('JOIN'),('KEY'),('KILL'),('LEFT'),('LIKE'),('LINENO'),('LOAD'),('MERGE'),('NATIONAL'),('NOCHECK'),('NONCLUSTERED'),('NOT'),('NULL'),('NULLIF'),
('OF'),('OFF'),('OFFSETS'),('ON'),('OPEN'),('OPENDATASOURCE'),('OPENQUERY'),('OPENROWSET'),('OPENXML'),('OPTION'),('OR'),('ORDER'),('OUTER'),('OVER'),
('PERCENT'),('PIVOT'),('PLAN'),('PRECISION'),('PRIMARY'),('PRINT'),('PROC'),('PROCEDURE'),('PUBLIC'),('RAISERROR'),('READ'),('READTEXT'),('RECONFIGURE'),
('REFERENCES'),('REPLICATION'),('RESTORE'),('RESTRICT'),('RETURN'),('REVERT'),('REVOKE'),('RIGHT'),('ROLLBACK'),('ROWCOUNT'),('ROWGUIDCOL'),('RULE'),
('SAVE'),('SCHEMA'),('SECURITYAUDIT'),('SELECT'),('SEMANTICKEYPHRASETABLE'),('SEMANTICSIMILARITYDETAILSTABLE'),('SEMANTICSIMILARITYTABLE'),('SESSION_USER'),
('SET'),('SETUSER'),('SHUTDOWN'),('SOME'),('STATISTICS'),('SYSTEM_USER'),('TABLE'),('TABLESAMPLE'),('TEXTSIZE'),('THEN'),('TO'),('TOP'),('TRAN'),('TRANSACTION'),
('TRIGGER'),('TRUNCATE'),('TRY_CONVERT'),('TSEQUAL'),('UNION'),('UNIQUE'),('UNPIVOT'),('UPDATE'),('UPDATETEXT'),('USE'),('USER'),('VALUES'),('VARYING'),
('VIEW'),('WAITFOR'),('WHEN'),('WHERE'),('WHILE'),('WITH'),('WITHIN GROUP'),('WRITETEXT')
Select A.*
From INFORMATION_SCHEMA.COLUMNS A
Join #Reserved
on Column_Name = Word
The ISO standard is to query the INFORMATION_SCHEMA views for meta-data information. A more concise way is to list table/view columns in SQL Server is sp_help:
EXEC sp_help 'your-object-name-here';
In SSMS, you can highlight the desired object name in a query window and press ALT+F1 to execute sp_help for that object. With SQL Operations Studio, ALT-F12 will peek the object definition.
SELECT Distinct TABLE_NAME FROM information_schema.TABLES
Hope this helps and is similar for columns. Plus, I haven't seen any such list till now but surely will add in future once I see it.

How do you change the table_schema when the current table_schema is null?

I somehow managed to create a table in a database with a null table schema.
I can't query the table since it has no owner, and altering the table doesn't work for the same reason.
I would alter the table using:
ALTER SCHEMA null TRANSFER dbo.SubscriptionAnswerMR
But that doesn't work.
The information_schema.tables looks like this:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE
q_Profiles NULL SubscriptionAnswerMR BASE TABLE
So my question is: How do I change q_Profiles' table_schema?
SQL Server 2000 (edit)
Microsoft SQL Server Management Studio 2008R2
You should be able to verify that your table is fine by seeing the result of the following query:
SELECT u.name
FROM q_Profiles..sysobjects AS o
INNER JOIN q_Profiles..sysusers AS u
ON o.uid = u.uid
WHERE o.name = 'SubscriptionAnswerMR';
This should be dbo unless someone explicitly created them with a different owner or used sp_changeobjectowner. Which you can use if you find that sysobjects also has the wrong answer:
EXEC sp_changeobjectowner 'SubscriptionAnswerMR', 'dbo';
ALTER SCHEMA is not valid here because it was introduced in SQL Server 2005. Though it would be useful for you to describe what "doesn't work" means.
INFORMATION_SCHEMA is a horribly unreliable set of views as #Pondlife points out. Also see the following, which doesn't help you much in SQL Server 2000, but should help going forward:
The case against INFORMATION_SCHEMA views
Also as a side note you seem to be confused about tables and database. TABLE_CATALOG is the database, not the table.
Did you note this comment in the documentation?
Do not use INFORMATION_SCHEMA views to determine the schema of an
object. The only reliable way to find the schema of a object is to
query the sys.objects catalog view or use the OBJECT_SCHEMA_NAME
function.

How to grep SQL Server stored procedures?

I would like to run a standard grep over all stored procedures in a given SQL Server database (assume 2005 or later). I have found a variety of simple queries to list the names of stored procedures containing a specific object, e.g.
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%table_I_want_to_find%'
But what I really want is, like grep, to list the specific lines in the identified stored procedures (so I do not have to manually open each one and see if it is what I am looking for).
I am open to solutions in T-SQL or PowerShell, or even an off-the-shelf utility.
Use SQL Search from Red Gate. It's a free tool and is fantastic.
I am brand new to SQL Server, but the following seems to work great and has proved itself to be quite useful already. I am using SQL Server 2008.
select ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_BODY, ROUTINE_DEFINITION
from INFORMATION_SCHEMA.ROUTINES
where ROUTINE_DEFINITION like '%searchtext%'
order by ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, ROUTINE_DEFINITION;
For completeness, here are some similar queries you'll probably want to know about:
exec sp_help 'myTable'
select name
from sys.all_views
order by name;
select TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE
from INFORMATION_SCHEMA.TABLES
order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, TABLE_TYPE;
select TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, DATA_TYPE
from INFORMATION_SCHEMA.COLUMNS
where COLUMN_NAME = 'myColumn'
order by TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME;
You could use SMO to dump the stored procedure DDL to files, and then use grep on them.
There are some examples of extracting the DDL here (called Scripting).

Find all the varchar() fields in sql server?

Is it possible to find all the varchar() columns in my database?
I am using SQL Server 2008 and would like to get the list in SQL server management console.
JD.
Yep, this should work:
select * from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'varchar'
Try, this will give varchar and nvarchar, if the character_maximum_length column returns -1 then it is varchar(max) or nvarchar(max)
select * from
INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE in('varchar','nvarchar')
Another method not limited to Views or User tables that utilizes sys objects instead of information_schema.
Multiple questions previously answered regarding usage of sys vs information_schema.
SELECT OBJECT_NAME(c.OBJECT_ID) TableName, c.name ColumnName
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE t.name = 'varchar' --you can change text to other datatypes
ORDER BY c.OBJECT_ID;
GO
Original source utilized for Sql Server 2005, just executed on Sql Server 2016.
Reference : Pinal Dave (https://blog.sqlauthority.com)
https://blog.sqlauthority.com/2007/08/09/sql-server-2005-list-all-the-column-with-specific-data-types/

Resources