U-SQL Intellisense from Alias - azure-data-lake

If I use an Alias in U-SQL the intellisense doesn't prompt me for anything. For example,
#query =
SELECT PropertyOne,
PropertyTwo
FROM #some_table;
#intellisense_test =
SELECT PropertyOne
FROM #query;
In this case the intellisense will work. But in the example below it will not.
#intellisense_test =
SELECT q.PropertyOne
FROM #query AS q;
This creates a problem when doing anything but simple queries. Does anyone know a work around for this?

This is the typical "SQL" problem for intellisense. Since the intellisense parser is only able to use the bindings already seen, it does not know in the SELECT clause what the FROM clause contains. I asked them to check if there is a way to look ahead to the FROM clause in the SELECT case, but for now unfortunately it does not.

I found a work around to this when I had several joins in my FROM clause.
If you write the alias immediately followed by a square bracket intellisense returns the field list from the alias table.
For example:
#intellisense_test =
SELECT q[PropertyOne]
FROM #query AS q;
This works when writing the statement and is handy for speed of typing. However, you will need to go back and add the dots between alias and field or you'll get a compile error. This is also easily done when doing a vertical highlight with the mouse by holding the ALT key.
Not perfect, but it's a valid work around.
Hope this helps.

Related

Oracle sql: is there a way to compose queries using aliases for entire clauses?

Analyzing an Oracle DB of an application of mine, I always run queries ending with the very same "order by" clause, given that every table has a date type "DT_EXTRACTION" column.
Is there a way to define an alias for String "order by DT_EXTRACTION desc" (say, equals to $DD) and write my query like this?
select *
from foo
$DD;
Since you're using SQL Developer you could (ab)use substitution variables for this:
define DD='order by DT_EXTRACTION desc'
select * from your_table
ⅅ
but you'd have to either define that string in each script/session, or add it to a login script to make it always available (which you can choose from Tools->Preferences->Database).
That would work in SQL*Plus too.
SQL Developer also has 'snippets', which you can view and manage from the panel revealed by View->Snippets. You can add your own snippet for that order by clause, and can then drag-and-drop it from the snippets panel into your code wherever you need to use it. Not quite what you asked for but still useful. #thatjeffsmith has a write up with pictures, so I won't repeat those details here, since it's not quite what you need.
You may find code templates useful too. From Tool->Preferences->Database choose SQL Editor Code Templates, and define a new one for your string:
Then in the worksheet, type as far as:
select * from your_table DD
hit control-space and it will expand automatically to
select * from your_table order by dt_extraction desc

SQL - Extremely human readable, but I don't understand the concepts behind it

I seem to approach thinking about sql the wrong way. I am always writing things that do not work.
For example I need a variable. So i think:
DECLARE #CNT AS INT
SET #CNT = COUNT(DISTINCT database.schema.table.column)
Why doesn't this work...? I am using a fully qualified reference here, so the value I want should be clear.
DECLARE #CNT AS INT
SET #CNT = (SELECT COUNT(DISTINCT database.schema.table.column) FROM column)
This works... but why do I have to use select?
Does everything have to be prefaced with one of the DDL or DML statements?
Secondly:
I can't debug line by line because a sql statement is treated all as one step. The only way I can debug is if I select the innermost sub-query and run that, then include next outer sub query and run that, and so on and so forth.
Is there a locals window?
I've heard about set-based thinking rather than iterative thinking, I guess I am still iterative even for functional languages... the iteration is just from innermost parentheses to outermost parentheses, and applied to the whole set. but even here I run into trouble because I don't know which value in the set causes the error.
Sorry if this seems scatterbrained... I guess that just kinda reflects how I feel about it. I don't know how to architect a big stored procedure from lots of little components......Like in vba I can just call another sub-routine and make sure the variables I need are global.
tldr: Need the conceptual grounding / knowing what actually happens when I type something and hit F5
On Question #1, You need select because that's how SQL works. You've given it a name, but haven't told it what to do with that name (select it, update it, delete it?) Just saying the column name is not grammatically correct.
On #2, Yes, SQL is declarative, you're not telling it what to do, you're telling it what to return. It will retrieve the data in the order that is most efficient at that particular moment in time, Normally your sub-query will be the last thing to run, not the first.
Yes, you have to use SELECT in-order to fetch that data first and then assign it to variable. You can also do it like
DECLARE #CNT AS INT
SELECT #CNT = COUNT(DISTINCT `column`) FROM database.schema.table

SQL: Update table column passed as variable

Any idea how to create this function in t-sql?
Pseudo-code:
function( #table, #table_column )
{
update #table
set #table_column = replace(#table_column,',','')
where #table_column like '%,%'
}
Ideas I've tried:
Procedures: only take readonly tables (http://technet.microsoft.com/en-us/library/ms187926.aspx)
Functions: cannot do updates...
Any suggestions? Thanks everyone!
Update: I had a database with about 40 tables, each with columns that I needed to remove special characters (i.e., ","). Although it would be nice to create a function/procedure where I could give it the name and fix the column, I decided instead (based on the comments) to just write each update out. Perhaps I was just looking for too fancy of a solution to a relatively simple problem.
The only way to do this is dynamic SQL. Unless you are writing database tools, you rarely need to do this kind of thing. Are you sure your database is designed correctly? What is the problem you are trying to solve?
Why do you need a function?
Usually functions are applied to a column inside the select list, such as SELECT MYFUNC(COL1) FROM TAB1;
This can definitely be done in a Stored Procedure with dynamic SQL. You can even look at a return value for the number of rows updated.
I guess the main question is what is your business requirements??

Bring up toad intellisense

When typing a query in Toad it usually brings up an intellisense scrolling box to help with my typing.
But sometimes this does not appear. Is there a short cut (like in Visual Studio) to bring it up?
EDIT I found that the key mapping is CTRL+.
For a query like
SELECT * FROM Person AS P
WHERE P. -- I expect intellisense to show me the columns.
-- Sometimes it does not and pressing the CTRL+. does nothing
Pressing CTRL+. away from a query brings up a very long list of things like -
##CONNECTIONS ##CPU_BUSY ##CURSOR_ROWS ##DATEFIRST ##DBTS ##ERROR
Do I have the correct shortcut key?
Is the problem that Toad can't figure out the column names in the query above?
EDIT 2
Very strange behavior
If I have
USE DB1
SELECT * FROM Person AS P
WHERE P.--I get the intellisense
BUT if I have the following in the editor -
USE DB1
SELECT * FROM Person AS P
WHERE P.--I get the intellisense
USE DB2
SELECT * FROM Company AS C
WHERE C. -- No intellisense
Solution below in my own answer
I suggest you to use CTRL+T combination. I used it and it works. Also Dell official support agrees, here too. Toad is not the greatest software for best user experience :)
CTRL-TAB will bring it up for you.
hope this helps!
It seems if there is a second USE on the page intellisense will not work with queries below the second USE!
If I have
USE DB1
SELECT * FROM Person AS P
WHERE P.--I get the intellisense
BUT if I have the following in the editor -
USE DB1
SELECT * FROM Person AS P
WHERE P.--I get the intellisense
USE DB2
SELECT * FROM Company AS C
WHERE C. -- No intellisense
It seems if there is a second USE on the page intellisense will not work with queries below the second USE!
You have to complete first query to get intellisense in second one.
So if you complete first query i.e.
USE DB1
SELECT * FROM Person AS P
WHERE P.FirstName = ‘Sanket’
Then only you can get intellisense in second query.
USE DB2
SELECT * FROM Company AS C
WHERE C. --Get the intellisense

How to shorten paths when accessing linked-server objects in SQL

Is there a way to shorted access paths in SQL? I am thinking something similar to alias's but I don't know how to ask this question in google to get the appropriate application of alias'ing
This:
select * from ServerName.DBName.dbo.TableName
To:
declare #RDB as RemoteDatabaseObject
set #RDB = ServerName.DBName.dbo
select * from #RDB.TableName
I know this doesnt work, but I want to know if there is a way to alias objects that have long paths.
Maybe you are looking for SYNONYM?
http://msdn.microsoft.com/en-us/library/ms177544.aspx
Aliasing works for what you're wanting here.
SELECT * FROM ServerName.DBName.dbo.TableName AS myAlias
The cool thing is that you can almost always use AS in the middle of sql statements.
http://www.w3schools.com/sql/sql_alias.asp
Other than using dynamic SQL, I dont think of any way which makes this possible.