query views code (definitions) - database

I am trying to find some views which have a comment in them - in the code! - (something like '--TO DO'), but the problem is that I don't know the views names and to look at each by hand would take me a whole lot of time (I have over 2k views).
So I am trying to make a query that will search in the view's code for the text of interest.
I managed to come up with something like this:
SELECT *
FROM ALL_SOURCE
WHERE UPPER(text) LIKE UPPER('%my_text_here%')
ORDER BY name desc
But this doesn't query my views. It queries the functions, the procedures, packages, triggers...pretty much everything except the views...which I find pretty odd.
At first I thought that maybe there were no views that contained that keyword so I changed to some basic SQL keywords that I knew for sure exist...and still no results.
Anyone could tell me what am I missing/doing wrong?
Thanks!

The views are stored in all_views (also: dba_views or user_views, depending on your access and needs). However, the text column in all_views is a long, which means you'll first have to convert it to a Clob before you can use it in your query. The easiest work around (from Tom Kyte) would be to create a new table in your schema and convert the Longs into Clobs like this:
create table myviews as
select owner, view_name, to_lob(text) as text from all_views;
Then select from your own table:
select *
from myviews
where upper(text) like upper('%my_text_here%')
order by view_name desc;

Related

Most efficient way to view data in table in SQL Server?

Basically if I want to see what the data in a table/view looks like I use
select top 1000 * from ...
But this isn't too efficient for complex views or badly indexed tables.
I really just want to see what the data in a table looks like, e.g. the format etc.
Is there a better way to do this?
I'm using SSMS 2017
*Edit for clarification:
Badly written views are endemic throughout our databases so whilst fixing these is the obvious answer it's not really a realistic one.
I suppose i was hoping for a trick i wasn't aware of, because i understand using TOP puts some sort of order into it
If you highlight a table/view object in code and then press ALT + F1 in SSMS IDE it will execute the equivalent command of sp_help ‘object_name’ where object_name is the name of the highlighted object. Maybe this can give some quick information about the object you are interested in.

SQL query getting data from 2 different database objects

I am very much a beginner at more in depth SQL queries other then simple queries needed to more manage the server side then the database side so any help here is greatly appreciated.
I have a a database for a ticketing system where I need to take data from two different objects and combine the results into the results.
Looks something like this:
+Prof1
- Columns
*AssignedTech (tech assigned task)
*Matters (unique matter ID)
*Type (open, closed, development etc.)
+Matters
- Columns
*MatterNumber
What I am trying to do is get the Matter Number into the query like below but don't know other then perhaps dumping the results to Excel and filtering them from there to get that data into this query. Matters in the DBO corresponds to the unique Matter ID noted above. If i could even run the query below and then use those results to query against the Matters DBO to get the matter ID along with it.
select *
from AssignedTech
where Type like 'open%'
order by Matters
I Believe you want to do is a SQL Join! (I suppose that the Column Matters at Prof1 Table is a reference to the MatterNumber at Matters Table)...
So, to do the join, you must write a query like this:
SELECT A.*, B.MatterNumber
FROM Prof1 A, Matters B
WHERE A.Matters = B.MatterNumber AND A.Type like 'open%'
ORDER BY A.Matters;
Hope this help (also, that I understand you correctly)
Mureinik, I wish I could post a screenshot but hopefully this helps. The section of the DB Basically looks something like this
Example

Mule - Record cannot be mapped as it contains multiple columns with the same label

I need to do join query to MS SQL Server 2014 DB based on a column name value. The same query runs when doing query directly to DB, but when doing query through Mule I'm getting error. The query looks something like this :
SELECT * FROM sch.emple JOIN sch.dept on sch.emple.empid = sch.dept.empid;
The above query work fine while doing query directly to MS SQL Server DB, but gives the following error through mulesoft.
Record cannot be mapped as it contains multiple columns with the same label. Define column aliases to solve this problem (java.lang.IllegalArgumentException). Message payload is of type: String
Request you to please help me out.
Specify columns list directly:
SELECT e.<col1>, e.<col2>, ...., d.<col1>,...
FROM sch.emple AS e
JOIN sch.dept AS d
ON e.empid = d.empid;
Remarks:
You could use aliases instead of schema.table_name
SELECT * in production code in 95% cases is bad practice
The column that has duplicate is empid(or more). You could add alias for it e.empid AS emple_empid and d.empid AS dept_empid or just specify e.empid once.
To avoid specifying all columns manually, you could drag and drop them from object explorer to query pane like Drag and Drop Column List into query window.
Second way is to use plugin like Redgate Prompt to expand SELECT *:
Image from: https://www.simple-talk.com/sql/sql-tools/sql-server-intellisense-vs.-red-gate-sql-prompt/
Addendum
But the same query works directly.
It works because you don't bind them. Please read carefully link I provided for SELECT * antipattern and especially:
Binding Problems
When you SELECT *, it's possible to retrieve two columns of the same name from two different tables. This can
often crash your data consumer. Imagine a query that joins two
tables, both of which contain a column called "ID". How would a
consumer know which was which? SELECT * can also confuse views (at
least in some versions SQL Server) when underlying table structures
change -- the view is not rebuilt, and the data which comes back can
be nonsense. And the worst part of it is that you can take care
to name your columns whatever you want, but the next guy who comes
along might have no way of knowing that he has to worry about adding a
column which will collide with your already-developed names.
But the same query works directly.
by Dave Markle

Creating SQL Server JSON Parsing/Query UDF

First of all before I get into the question, I'll preface this with the fact that I know that this is a "bad" idea. But for business reasons it is something that I have to come up with a solution to, and I'm hoping that someone, somewhere might have some ideas on how to go about this.
I have a SQL Server 2008 R2 table that has a "OtherProperties" column. This column contains various other, somewhat arbitrary additional pieces of information that relate to the records. There is a business need to create a UDF that we can use to query the results, for example.
SELECT *
FROM MyTable
WHERE MyUDFGetValue(myTable.OtherProperties, "LinkedOrder[0]") IS NOT NULL
This would find a record where there was an array of LinkedOrder entries that contained a value at index 0
SELECT *
FROM MyTable
WHERE MyUDFGetValue(myTable.OtherProperties, "SubOrder.OrderId") = 25
This would find a property "orderId" and use its value in a comparison.
Anyone seen an implementation of this? I've seen implementations of functions. Like this JSONParser that take the values into a table which just will not get us what we need query wise. Complexity wise, I don't want to write a full fledged JSON parser, but I can if I need to.
Not sure if this will suit your needs but I read about a CLR JSON serializer/deserializer. You can find it here, http://www.sqlservercentral.com/articles/CLR/74160/
It's been a long time since you asked your question but there is now a solution you can use - JSON Select which provides various functions for different datatypes, for example the JsonInt() function. From one of your examples (assuming OrderId is an int, if not you could use a different function):
SELECT *
FROM MyTable
WHERE dbo.JsonInt(myTable.OtherProperties, 'SubOrder.OrderId') = 25
DISCLOSURE:
I am the author of JSON Select, and as such have an interest in you using it :)
If you cannot use SQL Server 2016 with built-in JSON support, you would need to use CLR e.g. JSONselect, json4sql, or custom code such as http://www.codeproject.com/Articles/1000953/JSON-for-SQL-Server-Part, etc.

SQL server search

I'm going to perform a search in my SQL server DB (ASP.NET, VS2010,C#), user types a phrase and I should search this phrase in several fields, how is it possible? do we have functions such as CONTAINS() in SQL server? can I perform my search using normal queries or I should work in my queries using C# functions?
for instance I have 3 fields in my table which can contain user search phrase, is it OK to write following sql command? (for instance user search phrase is GAME)
select * from myTable where columnA='GAME' or columnB='GAME' or columnC='GAME
I have used AND between different conditions, but can I use OR? how can I search inside my table fields? if one of my fields contains the phrase GAME, how can I find it? columnA='GAME' finds only those fields that are exactly 'GAME', is it right?
I'm a bit confused about my search approach, please help me, thanks guys
OR works fine if you want at least one of the conditions to be true.
If you want to search inside your text strings you can use LIKE
select * from myTable where columnA like '%GAME%' or columnB like '%GAME%' or columnC like '%GAME%'
Note that % is the wildcard.
If you want to find everything that begins with 'GAME' you type LIKE 'GAME%', if you allow 'GAME' to be in the middle you need % in both ends.
You can use LIKE instead of equals and then it can contain wildcard characters, so your example could be:
select * from myTable where columnA LIKE '%GAME%' or columnB LIKE '%GAME%' or columnC LIKE '%GAME%'
Further information may be found in MSDN
This is going to do some pretty heavy lifting in terms of what the database has to do though - I would suggest you consider something like full text search as I think it would more likely be suited to your scenario and provide faster results (of course, if you never have many records to search LIKE would probably do fine). Information on this is also in MSDN
Don't use LIKE, as suggested by other answers. It won't work with indexes, and therefore will be slow to return and expensive to run. Instead, you have two options:
Option 1: Full-Text Indexes
do we have functions such as CONTAINS() in SQL server?
Yes! You can use the CONTAINS() function in sql server. You just have to set up a full-text index for each of the columns you need to search on.
Option 2: Lucene.Net
Lucene.Net is a popular client-side library for searching text data that integrates closely with Sql Server. You can use it to make implementing your search a little easier.

Resources