How to execute table valued function in SQL Server - sql-server

I'm getting syntax error when trying to execute table valued function.
select * from fn_security(select R.rk from dbo.LINK R)
Error:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
What am I doing wrong? how to execute this?

You can't pass whole table,like the way,you are trying now..you can use cross apply to get all
you can try below
select * from dbo.LINK r
cross apply
dbo. fn_security(r.rk)

Related

Microsoft SQL server 2016, cannot create sequence

I have Microsoft SQL server 2016, I am using SSMS to do my DB work. I am trying to create sequences and I keep getting this error
CREATE SEQUENCE dbo.seq_changeid As int START WITH 1 INCREMENT BY 1;
Error
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'SEQUENCE'.
Msg 319, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
I have tried it in SQLCMD and it does not work there as well. This is local database that i am using for some testing. I login using SQL Server Authentication.
I even tried select * from sys.sequences and it errors
Msg 208, Level 16, State 1, Line 10
Invalid object name 'sys.sequences'

insert into select, working with two tables from different servers error

My code:
INSERT INTO lclTabla
SELECT *
FROM openquery([LD_DB_A0FCCD_ALDOLANCHO],
'SELECT *
FROM [DB_A0FCCD_aldolancho].[dbo].[servTable]')
WHERE
lclTabla.dni = [LD_DB_A0FCCD_ALDOLANCHO].[DB_A0FCCD_aldolancho].[dbo].[servTable].[dni]
is causing an error:
Msg 4104, Level 16, State 1, Line 17
The multi-part identifier "lclTabla.dni" could not be bound.
Msg 4104, Level 16, State 1, Line 17
The multi-part identifier "LD_DB_A0FCCD_ALDOLANCHO.DB_A0FCCD_aldolancho.dbo.servTable.dni" could not be bound.
I want to insert where the id from local table is the same in the server table
Please help!!! In T-SQL
Whenever you run cross server query, You must follow proper naming convention
i.e. [server name].[database name].[schema name].[table name]
In your query, I can see wrong naming convention in -
"[LD_DB_A0FCCD_ALDOLANCHO].[DB_A0FCCD_aldolancho].[dbo].[servTable].[dni]"

Except operation in TSQL returning error

I need to apply an EXCEPT (same as MINUS in Oracle) operation in TSQL. I tried with a simple code
select * from Table1
except
select * from Table1 where calndr_dt between '2014-10-01' and '2014-10-10'`
Each sub query is executing fine. But when joined with EXCEPT, it is returning the following error message.
Msg 103010, Level 16, State 1, Line 1
Parse error at line: 2, column: 1: Incorrect syntax near 'except'.
Remove the ORDER BY clause from the first query, if you have this clause.

How can I move one of two perfectly identical rows into a new table through a query?

MS SQL Server 2008
I Use following Query to filter duplicate rows into a single row
Query 1 - SELECT DISTINCT * FROM flatfile_old
But now i want to move those query results into a new table and i try following things
Query 2 - SELECT * INTO flatfile_new FROM Flatfile_old WHERE 1 = 2
Query 3 - INSERT INTO flatfile_new (SELECT DISTINCT * FROM flatfile_old)
But Query No 3 throws error like follows
Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword
'select'. Msg 102, Level 15, State 1, Line 1 Incorrect syntax near
')'.
Please help
Just remove the brackets ( ... ) like this:
INSERT INTO flatfile_new
SELECT DISTINCT * FROM flatfile_old

SQL Server 2005 PIVOT Syntax Error

I receive the following error
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'SELECT'.
Msg 102, Level 15, State 1, Line 10
Incorrect syntax near ')'.
When I run the following query
SELECT TOP 100 *
FROM
(
SELECT [TimeStamp],[MeterID],[Value]
FROM access_AMIData) AS source
PIVOT
(
SUM(Value)
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
) as pvt
ORDER BY TimeStamp
The error comes with this line:
FOR MeterID IN (SELECT MeterNumber FROM access_tblFcppPvMeterList)
If I change this line to:
FOR MeterID IN (1,2,3,4)
It works perfectly...how can I specify a select Query inside the IN?
You can't, at least not in SQL 2005. You could include it in the WHERE clause of your inner query, but you have to hard-code the column names, unless of course you use dynamic SQL.

Resources