desc table in SQL Server? - sql-server

I want to see the definition of a table in SQL Server.
Running this query from SQLPro for MSSQL is OK
SELECT TOP 100 * FROM dbo.[ATRESMEDIA Resource Time Registr_];
but when I run this one
exec sp_columns dbo.[ATRESMEDIA Resource Time Registr_];
I got this error:
Msg 102, Level 15, State 1.
Incorrect syntax near '.'. (Line 3)

dont use schema dbo.
exec sp_columns [ATRESMEDIA Resource Time Registr_];
why? because, following are the parameters accepted by sp_columns stored proc:
sp_columns [ #table_name = ] object
[ , [ #table_owner = ] owner ]
[ , [ #table_qualifier = ] qualifier ]
[ , [ #column_name = ] column ]
[ , [ #ODBCVer = ] ODBCVer ]
source: msdn
update:
Martin's explanation as in comment:
Strings in SQL Server are delimited by single quotes - as a parameter to a stored proc in very limited circumstances it will allow you to skip the quotes but the dot breaks that. exec sp_columns 'dbo.[ATRESMEDIA Resource Time Registr_]'; wouldn't give the syntax error - but that wouldn't be what the proc expects anyway as the schema would need to be the second param

select the table name in the query window
and press the below key combination
Alt +F1 or
Alt+Fn+F1 will bring the table definition

Related

What is LEVEL equivalent in snowflake

I have to convert one oracle query to snowflake,which has a where clause LEVEL > 1. Could you please suggest me the best option.
Thanks.
I don't think it's an exact match, but the closest thing is the "start with" clause of Snowflake's connect by:
SELECT <column_list> [ , <level_expression> ]
FROM <data_source>
START WITH <predicate>
CONNECT BY [ PRIOR ] <col1_identifier> = [ PRIOR ] <col2_identifier>
[ , [ PRIOR ] <col3_identifier> = [ PRIOR ] <col4_identifier> ]
...
...
You can provide a where clause on the start with predicate, but without the "where" keyword. You can read more about it here: https://docs.snowflake.com/en/sql-reference/constructs/connect-by.html
There is level in snowflake. The differences from Oracle are:
In snowflake it's neccesary to use prior with connect by expression.
And you can't just select level - there should be any existing column in the select statement.
Example:
SELECT LEVEL, dummy FROM
(select 'X' dummy ) DUAL
CONNECT BY prior LEVEL <= 3;
LEVEL DUMMY
1 X
2 X
3 X
4 X

Use merge,the error message is There is an incorrect syntax near the keyword 'into'

I use merge like this:
BEGIN TRANSACTION
merge into TD_1 tar
using (select Title,AnnouncementID,SupplyTitle,EmployeeCode,registered,modified from TSupply_2 ) source
on (tar.SupplyTitle=source.SupplyTitle and tar.EmployeeCode=source.EmployeeCode)
when matched then update set tar.modified=getdate()
when not matched then
insert (Title,AnnouncementID,SupplyTitle,EmployeeCode,registered,modified)
values(source.Title,source.AnnouncementID,source.SupplyTitle,source.EmployeeCode,getdate(),getdate());
COMMIT TRANSACTION
But the error is:
There is an incorrect syntax near the keyword 'into'.
There is an incorrect syntax near 'source'.
How can I fix it?
The Merge Syntax:
MERGE <target_table> [AS TARGET]
USING <table_source> [AS SOURCE]
ON <search_condition>
[WHEN MATCHED
THEN <merge_matched> ]
[WHEN NOT MATCHED [BY TARGET]
THEN <merge_not_matched> ]
[WHEN NOT MATCHED BY SOURCE
THEN <merge_matched> ];
But you are using into:
merge into TD_1 tar
Also source is a reserved word of SQL. You can replace it if you want.
Try to take a look at this tutorial.
Now I know where is wrong,the COMPATIBILITY_LEVEL is 80 in my database.
So I can't use merge.
My database version is Microsoft SQL Server 2008
Then I modified COMPATIBILITY_LEVEL:
ALTER DATABASE DB1
SET COMPATIBILITY_LEVEL = 100
Then there is no error message.

Is it possible to change name of the system table

I want to change name of the system table in my database is it possible? Probably I shouldn't change it but I'm curious.
When I execute sp_rename I get the following error:
Msg 15001, Level 16, State 1, Procedure sp_rename, Line 404
Object 'cdc.[dbo_CdcTest_CT]' does not exist or is not a valid object for this operation.
Edit:
I want to change name of tables created by Change Data Capture because I want to disable CDC mechanism for table and still have data - I know that I can create additional table and move there data from CDC table but it's easier to change name of the CDC and then disable cdc for specified table.
No you cannot change the name of the system tables. However you can refer it with a different name.
You can use synonyms for that:
CREATE SYNONYM [ schema_name_1. ] synonym_name FOR <object>
<object> :: =
{
[ server_name.[ database_name ] . [ schema_name_2 ].| database_name . [ schema_name_2 ].| schema_name_2. ] object_name
}
Also to mention that sp_rename
Changes the name of a user-created object in the current database.
This object can be a table, index, column, alias data type, or
Microsoft .NET Framework common language runtime

DELETE FROM ... reporting syntax error at or near "."

I'm trying to delete just one data from my DB, but, when I write the command I keep getting that there's some syntax error, could you tell me where is the error?
This are the commands I've tried:
DELETE FROM database_userprofile WHERE user.username = 'some';
ERROR: syntax error at or near "."
LINE 1: DELETE FROM database_userprofile WHERE user.username = 'some'...
DELETE FROM database_userprofile USING database_user WHERE user.username="some";
ERROR: syntax error at or near "."
LINE 1: ... database_userprofile USING database_user WHERE user.username=...
Hope you can help me
Your query doesn't make any sense.
DELETE FROM database_userprofile WHERE user.username = 'some';
^^^^
Where'd user come from? It isn't referenced in the query. Is it a column of database_userprofile? If so, you can't write user.username (unless it's a composite type, in which case you would have to write (user).username to tell the parser that; but I doubt it's a composite type).
The immediate cause is that user is a reserved word. You can't use that name without quoting it:
DELETE FROM database_userprofile WHERE "user".username = 'some';
... however, this query still makes no sense, it'll just give a different error:
regress=> DELETE FROM database_userprofile WHERE "user".username = 'some';
ERROR: missing FROM-clause entry for table "user"
LINE 1: DELETE FROM database_userprofile WHERE "user".username = 'so...
My wild guess is that you're trying to do a delete over a join. I'm assuming that you have tables like:
CREATE TABLE "user" (
id serial primary key,
username text not null,
-- blah blah
);
CREATE TABLE database_userprofile (
user_id integer references "user"(id),
-- blah blah
);
and you're trying to do delete with a condition across the other table.
If so, you can't just write user.username. You must use:
DELETE FROM database_userprofile
USING "user"
WHERE database_userprofile.user_id = "user".id
AND "user".username = 'fred';
You'll notice that I've double-quoted "user". That's because it's a keyword and shouldn't really be used for table names or other user defined identifiers. Double-quoting it forces it to be intepreted as an identifier not a keyword.
Due to documentation, the syntax for delete in PostgreSQL 9.1 is:
[ WITH [ RECURSIVE ] with_query [, ...] ]
DELETE FROM [ ONLY ] table [ * ] [ [ AS ] alias ]
[ USING using_list ]
[ WHERE condition | WHERE CURRENT OF cursor_name ]
[ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ]
So you need to specify the "table_name" after DELETE command, not the "database_name".
You can delete data only if you are logged into the database.
You got
ERROR: syntax error at or near "."
because in the WHERE section you can specify the target table or the tables in the usinglist.
You may also get this error when copy-pasting a query from Eclipse to pgadmin. Somehow, a strange symbol may be inserted. To avoid this error, paste it in a simple text editor first (like notepad), then cut it from there and paste it in pgadmin.

Why won't this SQL statement run

SELECT * FROM agency
INNER JOIN TUser
[agency].[dbo].[Matrix_Branch_ID]=[TUser].[dbo].[client_id]
Microsoft SQL Server Managment Studio gives me:
SQL Server Management Studio gives me:
Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '.'.
Edit
After fixing the syntax errors with
SELECT * FROM agency
INNER JOIN TUser
ON dbo.agency.Matrix_Branch_ID=dbo.TUser.client_id
SQL Server Management Studio now gives me:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'agency'.
Msg 208, Level 16, State 1, Line 1
Invalid object name 'TUser'.
If you're running this in SSMS - are you in the right database; the one that contains those two tables?
You can see that current database you're in when a query window is active - both in a drop-down on the toolbar, as well as the query window's footer.
You're missing the ON keyword See the <joined table> grammar in FROM (Transact-SQL)
<joined_table> ::= {
<table_source> <join_type> <table_source> ON <search_condition>
| <table_source> CROSS JOIN <table_source>
| left_table_source { CROSS | OUTER } APPLY right_table_source
| [ ( ] <joined_table> [ ) ] }
Also the [dbo] between what I presume is the table and fields names is wrong. See Using Identifiers As Object Names
this should work
SELECT * FROM agency
INNER JOIN TUser
ON [agency].[Matrix_Branch_ID]=[TUser].[client_id]
Are you selecting the correct database? SQL Server defaults to using the master database, which is not the one you probably want.
You probably want to qualify the schema on the table, not on the items you are selecting. For instance:
Use [Database_name]
SELECT * FROM dbo.agency
INNER JOIN dbo.TUser ON agency.Matrix_Branch_ID=TUser.client_id

Resources