sql view refresh with period - sql

I'm able to create a sqlview with peroid(.)
create view [a.b] as select 1 as A
but sp_refreshview [a.b] gives error:
Msg 15165, Level 16, State 1, Procedure sp_refreshsqlmodule_internal, Line 55
Could not find object 'a.b' or you do not have permission.
I tried sp_refreshview 'a.b', no luck
How to do sp_refresh in this case?

You need to wrap the view name in single quotes: sp_refreshview '[a.b]'

Related

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]

Invalid object name when updating after renaming table

Have a very strange problem when trying to rename table and use it.
I have a table called oldTable and rename it to the newTable.
I can successfully use select on this table using:
SELECT * FROM database.dbo.newTable;
But when I try to use update like this:
UPDATE database.dbo.newTable SET foo = bar where id = 1;
I receive following error:
Msg 208, Level 16, State 1, Procedure archive, Line 4 [Batch Start Line 0]
Invalid object name 'oldTable'.
Looks like name oldTable was stored somewhere and is used here by some kind of reference. It happens on both ssms and raw php+sql when trying to update.
Anyone have any idea?
Renaming a table will not update any Triggers that have been defined for the table (or any references to it anywhere else) so you need to manually update any triggers or other dependencies to reflect the new name.

Table structure using sp_help

When I tried this query:
sp_help '[OG_System].[dbo].[tbl_act]'
This message appears:
Msg 15250, Level 16, State 1, Procedure sp_help, Line 48
The database name component of the object qualifier must be the name of the current database.
Because your target database is different than current database.
Use;
EXEC [ServerName].[DatabaseName].dbo.sp_help 'tbl_act'
thanks goGud, I know the correct usage of sp_columns.
EXEC [ServerName].[DatabaseName].dbo.sp_columns 'tbl_act'

Syntax error trying to see sys.dm_fts_index_keywords

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.