Why CONTAINS method doesn't work with REPLACE - sql

I'm using SQL Server 2014 and I'm trying to execute a query with REPLACE method within a CONTAINS method like this:
SELECT *
FROM A
WHERE CONTAINS(Name, REPLACE('abcd', 'a', 'b'))
But the query returns an error
Incorrect syntax near 'REPLACE'.
How can I do it correctly?

You can write as below:
declare #param nvarchar(100)='abcd';
set #param= REPLACE(#param,'a','b');
SELECT *
FROM PersonAddress
WHERE CONTAINS(FullName, #param)
But first, You need to do this

Try with cte
with cte as
(
select
*
from A
where REPLACE('abcd', 'a', 'b') as col
)
select
*
from cte
where contains(col, name)

Related

Is it possible to use SQL Update on a Sub Query

Is it possible to use the sql UPDATE on a sub query? I am using Big
Query Standard SQL and have tried every permutation of the below that I can come up with:
WITH test AS (SELECT * FROM 'my.database.table'),
test2 AS (UPDATE test SET myField = 100 WHERE myField < 100)
SELECT * FROM test2
I always receive the error:
Syntax error: Expected "(" or keyword SELECT or keyword WITH but got
keyword UPDATE
Use below instead
with test as (
select * from `my.database.table`
), test2 as (
select * replace(greatest(myfield, 100) as myfield) from test
)
select * from test2

Multiple Linked Servers in one select statement with one where clause, possible?

Got a tricky one today (Might even just be me):
I have 8 Linked SQL 2012 servers configured to my main SQL server and I need to create table views so that I can filter all these combined table results only using one where clause, currently I use UNION because they all have the same table structures.
Currently my solution looks as follows:
SELECT * FROM [LinkedServer_1].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_2].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_3].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_4].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_5].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_6].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_7].[dbo].[Table] where value = 'xxx'
UNION
SELECT * FROM [LinkedServer_8].[dbo].[Table] where value = 'xxx'
As you can see this is becoming quite ugly because I have a select statement and where clause for each linked server and would like to know if there was a simpler way of doing this!
Appreciate the feedback.
Brakkie101
Instead of using views, you can use inline table-valued functions (a view with parameters). It will not save initial efforts for creating the queries, but could save some work in the future:
CREATE FUNCTION [dbo].[fn_LinkedSever] (#value NVARCHAR(128))
AS
RETURNS TABLE
AS
RETURN
(
SELECT * FROM [LinkedServer_1].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_2].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_3].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_4].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_5].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_6].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_7].[dbo].[Table] where value = #value
UNION
SELECT * FROM [LinkedServer_8].[dbo].[Table] where value = #value
);
Also, if possible, use UNION ALL instead of UNION.

PostgreSQL WITH .. AS () difficulties

I am trying to use postgresql WITH AS () construction but I got error:
Even on simple queries like:
WITH a AS (
SELECT '2'
)
SELECT a
I got:
-->> ERROR: column "a" does not exist LINE 4: SELECT a
Where am i wrong? Thanks.
The easiest and most useful is to declare the column name:
with a(a) as (select '2')
select a from a;
But if you just select the table:
with a as (select '2')
select a from a;
a
-----
(2)
It will return the row valued type a which may or not be useful for you.
Try this sql code:
WITH a AS (
SELECT '2'
)
SELECT * FROM a;

Decode function for selecting Tables

Is there any (same or alternate) way of using Table names with the DECODE function.
Something like this:
SELECT * FROM
DECODE(FLAG,1,TABLE1,2,TABLE2,TABLE3)
In SQL the table names cannot be determined at the run time. They need to be provided at the compile time.
If you need to provide the table names dynamically, you will need to use Dynamic SQL.
Hope it Helps
Vishad
select * from table1 where decode(flag, 1,1, 0) = 1 union
select * from table2 where decode(flag, 2,1, 0) = 1 union
select * from table3 where decode(flag, 1,0, 2,0, 1) = 1;

Stored Procedure return multiple result sets

I need a SP to return multiple sets of results. The second set of results would be based on a column of the first set of results.
So:
declare #myTable1 table(field0 int,field1 varchar(255))
insert into #myTable1 select top 1 field0, field1 from table1
declare #myTable2 table(field0 int,field3 varchar(255))
insert into #myTable2
select field0, field3 from table2
where #myTable1.field0 = #myTable2.field0
How do return #myTable1 and #myTable2 with my SP? Is this syntax even right at all?
My apologies, I'm still a newbie at SQL...
EDIT:
So, I'm getting an error on the last line of the code below that says: "Must declare the scalar variable "#myTable1""
declare #myTable1 table(field0 int,field1 dateTime)
insert into #myTable1
select top 1 field0, field1
from someTable1 m
where m.field4 > 6/29/2009
select * from #myTable1
select *
from someTable2 m2
where m2.field0 = #myTable1.field0
If I highlight and run the code up until the second select * it works fine...
when I highlight the rest it acts like the first variable doesn't exist...
EDIT2:
Figured that problem out. Thanks guys.
declare #myTable1 table(field0 int,field1 dateTime)
insert into #myTable1
select top 1 field0, field1
from someTable1 m
where m.field4 > 6/29/2009
select * from #myTable1
select *
from someTable2 m2
where m2.field0 = (select field0 from #myTable1)
You pretty much just select two result sets
SELECT * FROM #myTable1
SELECT * FROM #myTable2
However, some tools will hide some results (e.g. pgAdmin will only show the last) and some tools have some sort of requirement to get to the next result set (e.g. .NET's IDataReader's will not allow you to Read() from the second resultset until you call NextResult()).
Edit:
An alternative in this case, since the types of the two results match, is to combine them into a single resultset:
SELECT field0, field1 from #myTable1
UNION
SELECT field0, field3 from #myTable2
You can also choose between UNION ALL or UNION DISTINCT (the default) where the latter will only send rows that aren't repeats.
At the end of the Stored Proc, put:
SELECT * FROM #myTable1
SELECT * FROM #myTable2
This will return 2 result sets.