Table structure using sp_help - sql

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'

Related

Get list of SQL server databases which files have been deleted

My goal is getting list of SQL server databases which files have been deleted.
In other words. I attach a db from mount, then close the mount so actually I have the attached db without files.
At first it seemed to be easy. Just pretty easy select:
SELECT
'DB_NAME' = db.name,
'FILE_NAME' = mf.name,
'FILE_TYPE' = mf.type_desc,
'FILE_PATH' = mf.physical_name
FROM
sys.databases db
INNER JOIN sys.master_files mf
ON db.database_id = mf.database_id
WHERE
--and specific condition here
But it turned out differently. Sql server has almost the same information about a regular database and a database which doesn't have files. So I had to try something else.
Further I tried to use state of database. And it was quite strange.
Unfortunately the following query gives me wrong(or not actual information):
SELECT state
FROM sys.databases
WHERE name = N'TestDB'
state
-----
0
And 0 means ONLINE according to this link
But actually the database has RECOVERY_PENDING state. It looks like that sql server information about my TestDB us out of date and should be refreshed. But I have no idea how to achieve this. But after executing any of following query this info(db state) is being refreshed:
EXEC sp_helpdb N'TestDB'
ALTER DATABASE N'TestDB' SET SINGLE_USER WITH ROLLBACK IMMEDIATE
USE N'TestDB'
--etc
--all requests are terminated with the same error
Msg 5120, Level 16, State 101, Line 10
Unable to open the physical file "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB.mdf". Operating system error 3: "3(The system cannot find the path specified.)".
File activation failure. The physical file name "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB_log.ldf" may be incorrect.
File activation failure. The physical file name "C:\MOUNT\b4c059e8-3ba6-425f-9a2a-f1713e7719ca\TestDB_log-2.ldf" may be incorrect.
Msg 5181, Level 16, State 5, Line 10
Could not restart database "TestDB". Reverting to the previous status.
Msg 5069, Level 16, State 1, Line 10
ALTER DATABASE statement failed.
So do you have any idea ?
And also I've asked the question looks like this here differently.
Finally, I've found what i actually need.
I can chech whether the specific file exists or not by sql server:
CREATE FUNCTION dbo.fn_FileExists(#path varchar(512))
RETURNS BIT
AS
BEGIN
DECLARE #result INT
EXEC master.dbo.xp_fileexist #path, #result OUTPUT
RETURN cast(#result as bit)
END;
GO
So i just need to execute the function above for each file which i can get by executing for example following query:
SELECT
DISTINCT 'FILE_PATH' = physical_name
FROM sys.master_files

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]

SQL Server could not find stored procedure 'show'

I can't use the most basic procedure, show, as it throws an error:
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'show'.
I used:
SHOW DATABASES
Please help me find answers to this issue.
To list all Databases in SQL Server,
Select * from sys.databases
To exclude in-built DBs,
Select * from sys.databases WHERE name NOT IN ('master', 'tempdb', 'model', 'msdb');
Try to refresh local Cache which is under the Intellisense.

sql view refresh with period

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]'

OPENQUERY throws error when used with WIN2K8\SQL2K12

I'm trying the following Sql query to move my stored procedure result into table
SELECT *
INTO #tmpTable
FROM OPENQUERY(WIN2K8\SQL2K12, 'EXEC vcs_gauge #gauge_name=vs1_bag,#first_rec_time=2014-09-01 09:00:00,#last_rec_time=2014-09-01 10:00:00')
following error is thrown, when I execute the query.
Incorrect syntax near '\'.
I don't want to add linked server .How to resolve this issue?
EDIT1
When I do [win2k8\sql2k12], and first execute the following command
EXEC sp_serveroption 'YourServer', 'DATA ACCESS', TRUE
A new message comes
OLE DB provider "SQLNCLI11" for linked server "WIN2K8\SQL2K12" returned message "Deferred prepare could not be completed.".
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '-'.
You need to enclose DATETIME values in single quotes. And since your query is in a string itself, those single-quotes need to be doubled / escaped as follows (and you should probably also put the first parameter's value in escaped-single-quotes as it is clearly a string).
You should also fully qualify the stored procedure name with [DatabaseName].[SchemaName]..
And since the vcs_gauge proc uses Dynamic SQL, you need to specify the WITH RESULT SETS clause. For more info on this clause, please see the MSDN page for EXECUTE.
SELECT *
INTO #tmpTable
FROM OPENQUERY([WIN2K8\SQL2K12],
N'EXEC [DatabaseName].[SchemaName].vcs_gauge
#gauge_name = ''vs1_bag'',
#first_rec_time = ''2014-09-01 09:00:00'',
#last_rec_time = ''2014-09-01 10:00:00''
WITH RESULT SETS ( { column_specification} );
');