Syntax error trying to see sys.dm_fts_index_keywords - sql

Trying to run
SELECT *
FROM sys.dm_fts_index_keywords( DB_ID('database_name'), OBJECT_ID('table_name') )
against the table with the index and I get a syntax error
If I run in on master I get
Msg 30007, Level 16, State 1, Line 1
Parameters of dm_fts_index_keywords and dm_fts_index_keywords_by_document cannot be null.

I had to upgrade the compatibility level for the Database in order to be able to create the stoplist. Then all worked fine. Edetails here http://blogs.msdn.com/b/sqlserverfaq/archive/2011/08/31/creating-stoplist-on-database-of-compatibility-80-90-in-sql-server-2008.aspx.

Related

sp_refreshsqlmodule is failing with UNION, INTERSECT or EXCEPT operator must have an equal number of expressions error

I have written one stored procedure where I have added UNION like this:
SELECT *,SysStartTime, SysEndTime FROM dbo.FirstTable WHERE Id = #Id
UNION
SELECT * FROM history.FirstTable WHERE Id = #Id
where dbo.FirstTable is temporal table and history.FirstTable is it's history table.
If I write a query like:
exec sp_refreshsqlmodule N'USP_MySPName'
It fails with below error:
Msg 205, Level 16, State 1, Procedure sys.sp_refreshsqlmodule_internal, Line 85 [Batch Start Line 0]
All queries combined using a UNION, INTERSECT or EXCEPT operator must have an equal number of expressions in their target lists.
However if I alter it, it doesn't show any error. And even while execution it doesn't show any error. Even if I execute the above query separately it works fine.
I tried searching for the cause of this error but I m not able to find any reference.
Can someone help me with the reason for this error.
NOTE: This error can be fixed with manually specifying all the column names from both tables instead if using *

Trying to add a column to SQL Server table

I am getting incorrect syntax errors with the following statement (SQL Server 2017)
USE ToDo
GO
ALTER TABLE tasks ADD COLUMN completed TINYINT(1);
Started executing query at Line 1
Commands completed successfully.
20:31:38Started executing query at Line 3
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'COLUMN'.
A very similar syntax was used to remove a column that worked fine.
This code would run in MySQL - but not on SQL Server. Consider:
alter table tasks add completed tinyint;
Rationale:
alter table does not support column in SQL Server; just remove that keyword
the tinyint datatype does not take a length

Can't drop a view inside a function

I'm trying to make a simple function to return a random eye color. I could do this in a stored procedure but I was trying to challenge my self and try something new. I'm getting some errors, I've tried this a few different ways and I have found some documents roughly related but I'm just not skilled enough I think to understand what I did wrong and what the documents I've found are referring too syntax wise. I hope that makes sense.
CREATE FUNCTION Random_Eyes ()
RETURNS NVARCHAR(100)
AS
BEGIN
CREATE VIEW Rand_Eyes
AS
SELECT TOP 1 [Eyes]
FROM [dbo].[Eyes]
ORDER BY NEWID()
GO
DECLARE #Eyes AS NVARCHAR(100) = (SELECT [Eyes] FROM Rand_Eyes)
RETURN #Eyes
DROP VIEW Rand_Eyes
END
GO
Errors:
Msg 156, Level 15, State 1, Procedure Random_Eyes, Line 14
Incorrect syntax near the keyword 'VIEW'.
Msg 102, Level 15, State 1, Procedure Random_Eyes, Line 14
Incorrect syntax near ')'
Msg 178, Level 15, State 1, Line 4
A RETURN statement with a return value cannot be used in this context.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'END'.
Any feedback or suggestions would be helpful. Thank you
You don't need a temporary view inside a function - that's not even allowed anyway as functions in SQL Server are strictly read-only: they cannot perform any DDL operations (CREATE, UPDATE, INSERT, DELETE).
Note that ORDER BY NEWID() is not the best way to get a random result (because it triggers a table linear scan). Try this:
CREATE FUNCTION Random_Eyes()
RETURNS NVARCHAR(100)
AS
BEGIN
DECLARE #count int = ( SELECT COUNT(*) FROM [Eyes] )
SELECT
Eyes
FROM
dbo.Eyes
ORDER BY
EyeId -- or whatever your constant, incrementing primary-key is
OFFSET
( RAND() * #count ) ROWS
FETCH NEXT
1 ROWS ONLY
END

View Statement on SQL Server returns "Invalid Object Name"

When executing the following script on my SQL Server:
CREATE VIEW joiny AS
SELECT EventTime
FROM [dbo].[Table_1]
I get the following error:
Invalid object name 'Table_1'.
I cannot figure out why this is an error. Could anyone point me in the right direction? I tried with and without the [] as well as the "dbo".
This error can mean that the table [dbo].[Table_1] simply does not exist in the database you are using.
CREATE VIEW tabtest
AS
SELECT *
FROM dbo.TabTest
Results in:
Msg 208, Level 16, State 1, Procedure tabtest, Line 4 Invalid object
name 'dbo.TabTest'.
Because I do not have a table named dbo.tabtest in my database.\
If you are trying to create a view in a different database than the one where your table exists, then you need to use 3-part naming when you specify the table, like this:
CREATE VIEW joiny AS
SELECT EventTime
FROM [MyDatabaseName].[dbo].[Table_1]

Why would I get a syntax error on this pretty simple trigger?

I'm getting a syntax error :
Msg 156, Level 15, State 1, Procedure trgDiscoverSurchargeChangeTiming, Line 26
Incorrect syntax near the keyword 'select'
on a trigger I'm writing. Now, I normally avoid triggers because I tend to forget them later when I am making changes to the DB, so I'm a little rusty on them, but I feel like this one should be fine:
(TRIGGER STUFF)Begin
if update(surchargepay)
begin
insert into
dbo.CustomErrorLog
(errorText
, ErrorOrderID
, errorOldValue
, errorNewValue)
values
select -- This is where the error is being thrown
convert(varchar(50), getdate())
, i.routeid
, d.surchargepay
, i.surchargepay
from
inserted i INNER JOIN
deleted d on i.id = d.id
end
End
any ideas what might cause that?
You don't need the "value" keyword when using "select" to populate an insert.