I am working on what should be a super simple query for SQL Server 2014. All I want to do is check that our systems can interface with SQL Server after updates, etc. So I need to just verify that it makes the connection correctly and finds a table within the Server.
Attempt 1:
SELECT TOP (1) *
From [X].[dbo].[Y]
WITH (NOLOCK);
But apparently 'top' is not a supported option with SQL Server 2014.
To add some more, here is the exact error I get when trying to run that: Syntax error. The token 'Top' is invalid. Please check the case of your operators (eg 'or' versus 'OR') and check that your functions use brackets after the function name eg Now(), eg Len("abc").
Attempt 2:
SELECT *
From [X].[dbo].[Y]
WITH (NOLOCK)
LIMIT (1);
That one tells me that I need to put data items between [], text between "", and functions as FunctionName(). However...I don't see where I missed any of those.
Can anybody possibly shed some light on why my query isn't going through? Any help would be appreciated.
The first attempt should work just fine:
SELECT TOP (1) *
From [dbo].[Y]
WITH (NOLOCK);
See example
If it doesn't work, you should include the error message.
I am stuck with a regular expression in SQL server 2005+, i.e. I need a regular expression to validate a first name (which allows only alphabets,whitespaces and a .(dot)).
I tried with below query
SELECT PATINDEX('%[A-Z]%[a-z]%[.]%','John H. Wilson') as VALIDFIRSTNAME
But, this also fails in some cases. I'm unable to find a clear regular expression. Any assistance would be very much appreciated.
I have used patindex to recognise the given pattern. If the string doesn't match the pattern, then It should give 0 else it should give 1 or >1.
Thanks in advance.
You can't match mentioned condition with available patterns.
Try to follow this way.
I am trying to get the text elements of a table that follows a paragraph that contains a specific text element using XQuery on MS SQL Server. The problem is whenever I use the axes "following", "following-sibling" or "previous-sibling", I get an error saying this is not supported in SQL Server (I'm using version 2008). So for instance, I can get the first paragraph node that contains a text node whose value is "blah":
//w:p[descendant::w:t = "blah"]
And I can get the text from a table element using:
//w:tbl//w:t/text()
I don't see any way I can force the query to only return the first table element that follows the previously captured paragraph node since:
//w:tbl[following:://w:p//w:t = "blah"]//w:t/text()
Gives the error: "XQuery [Specification.document.query()]: The XQuery syntax 'following' is not supported."
And the same for:
//w:tbl[following-sibling::w:p[descendant::w:t = "blah"]]//w:t/text()
Gives "XQuery [Specification.document.query()]: The XQuery syntax 'following-sibling' is not supported."
That ain't right, y'all know! XPath has supported following and following-sibling since 1.0 back in 1999 AFAICT so MS SQL Server seems to be severely deficient in terms of standard compliance but either way, does anyone see a way I can do this without those axes? Thanks in advance!
Document order of nodes in XQuery can also be evaluated by using node comparison operators.
Operator >> applies to two nodes and returns true if the left hand side node follows the right hand side node in document order. For solving your problem, you'd select the first such node.
In the following code, $blah and $text are the given expressions. The returned value is the first node in $text that follows the first node in $blah.
let $blah := //w:p[descendant::w:t = "blah"]
let $text := //w:tbl//w:t/text()
return $text[. >> $blah[1]][1]
Or, combined into a single expression,
(//w:tbl//w:t/text()[. >> (//w:p[descendant::w:t = "blah"])[1]])[1]
I'm writing a fairly complex stored procedure to search an image library.
I was going to use a view and write dynamic sql to query the view, but I need to use a full text index, and my view needs outer joins (MS-SQL 2005 full-text index on a view with outer joins)
So, I'm back to a stored procedure.
I need to search on (all optional):
a general search query that uses the full text index (or no search terms)
one or more categories (or none)
a single tag (or none)
Is there a way to do a conditional FREETEXT in the 'WHERE' clause? The query may be empty, in which case I want to ignore this, or just return all FTI matches.
...AND FREETEXT(dbo.MediaLibraryCultures.*, '"* "') doesn't seem to work. Not sure how a case statement would work here.
Am I better off inserting the category/tag filter results into a temp table/table variable, then joining the FTI search results? That way I can only do the join if the search term is supplied.
Thoughts?
I know it's a year later and a newer version of SQL but FYI...
I am using SQL Server 2008 and have tried to short circuit using
AND ( #searchText = '' OR freetext(Name, #searchText))
and I receive the message "Null or empty full-text predicate" when setting #searchText = ''. I guess something in 2008 has changed that keeps short circuiting from working in this case.
You could add a check for the empty search string like
where ...
AND (FREETEXT(dbo.MediaLibraryCultures.*, #FreeTextSearchFor) OR #FreeTextSearchFor = '')
(I have a feeling that freetext searches can't have null passed into them, so I'm comparing to an empty string)
If the term to search for is empty, the whole clause will evaluate to true, so no restrictions will be applied (by this clause) to the rows returned, and of course since its a constant being compared to a variable - I would think the optimizer would come into play and not perform that comparison for each row.
Hmm, I thought there was no short-circuiting in sql server?
AND (#q = '' OR FREETEXT(dbo.MediaLibraryCultures.*, #q))
seems to work just fine!
Strangely, the full text scan is still part of the execution plan.
Doesn't work on SQL Server 2014. I tried the suggested short circuit in one of my stored procedures, but it keeps evaluating the FREETEXT expression. The only solution I have found is the following:
IF ISNULL(#Text, N'') = N'' SET #Text = N'""'
SELECT ...
WHERE ...
AND (#Text = '""' OR FREETEXT([Data], #Text)
In Management Studio, you can right click on the tables group to create a filter for the table list. Has anyone figured out a way to include multiple tables in the filter? For example, I'd like all tables with "br_*" and "tbl_*" to show up.
Anyone know how to do this?
No, you can't do this. When we first got Management Studio I've tried every possible combination of everything you could think of: _, %, *, ", ', &&, &, and, or, |, ||, etc...
You might be able to roll your own addon to SMSS that would allow you to do what you are looking for:
The Black Art of Writing a SQL Server Management Studio 2005 Add-In
Extend Functionality in SQL Server 2005 Management Studio with Add-ins
The first one is specifically for searching and displaying all schema objects with a given name so you might be able to expand upon that for what you are looking for.
I'm using SQL Server Management Studio v17.1 and it has a SQL injection bug in it's filter construction, so you can actually escape default
tbl.name like '%xxx%'
and write your own query (with some limitations). For example to filter tables that are ending with "_arch", "_hist", "_purge" I used following filter value
_arch') and RIGHT(tbl.name, 5) != N'purge' and RIGHT(tbl.name, 4) != N'hist' and not(tbl.name like N'bbb
You can use SQL Server Profiler to see the constructed query and adjust it as needed.
Not sure if this same bug is available in previous SQL Management Studio versions or when it will be fixed, but for now I'm happy with the result.
I've used Toad for SQL Server (freeware version) which has very nice filtering options.
At first it looks like it could use a CONTAINS query (e.g. "br_*" OR "tbl_*"), but it doesn't seem to. It seems to only support a value that is then passed into a LIKE clause (e.g. 'app' becomes '%app%').
The "sql injection" method still works (v17.5), but with a twist:
zzzz' or charindex('pattern1', name) > 0 or charindex('pattern2', name) > 0 or name like 'zzzz
(I used the 'zzzz' to bypass the '%')
It doesn´t work if '_' or '%' is used in the patterns (or anywhere on your code), because it will automatically be replaced by '[_]' or '[%]' before evaluation.
As others have said, you cannot do this in SQL Server Management Studio (up and including 2014).
The following query will give you a filtered list of tables, if this is all you need:
SELECT
CONCAT(TABLE_SCHEMA, '.', TABLE_NAME) AS TABLE_SCHEMA_AND_NAME,
TABLE_SCHEMA,
TABLE_NAME
FROM
INFORMATION_SCHEMA.TABLES
WHERE
TABLE_SCHEMA IN ('X', 'Y', 'Z') -- schemas go here
ORDER BY
TABLE_SCHEMA,
TABLE_NAME;
The SQL injection method still works (somewhat) as of SSMS 2017 v17.8.1, although it puts brackets around the % symbol, so it will interpret those literally.
If you're using the Name->Contains filter, Profiler shows:
... AND dtb.name LIKE N'%MyDatabase1%')
So, in the Name->Contains field: MyDatabase1') OR (dtb.name LIKE 'MyDatabase2 should do it for simple cases.
This is old I know, but it's good to know that it can works if you input just entering the "filter" text. Skip * or % or any other standard search characters, just enter br_ or tbl_ or whatever you want to filter on.
Your in luck, I just conquered that feat, although my success is small because you can filter by schema which would allow you see more than 1 table but you have to type the filter text in each time you want to change it.