How to view a normal table in SQL - sql

SQL using sp_HelpText to view a stored procedure is used to view a stored procedure but what if i want to view my table,
is there any query to view my table? (I am newb to Sql and C#)

You can use sp_help "table_name" or select table & press Alt+F1 to view details of table..To View script, you need to script that using SQL server Management Studio.
Expand Database > Tables > Right Click on the table > Click on Create Script.
Thanks

I would suggest using the native INFORMATION_SCHEMA views to get table information. The following query contains a complete list of columns, data types etc for a given table without needing to join several sys views.
SELECT
*
FROM
INFORMATION_SCHEMA.COLUMNS
WHERE
[TABLE_NAME] = 'YourTable'

The table details can be viewed by pressing Alt+F1 after highlighting the table name.

Related

System table or stored procedure in Netezza to get create-table-sql of a table

I need to extract the create-table-sql of a table created in a database in Netezza, by using a query executed from a program. If there is any system table, stored procedure or otherwise, which let's me do that, please let me know. Your help will be highly appreciated.
The create statements are not saved in a table, however you can query the table structure from the system tables in you database. A good start is NFORMATION_SCHEMA.COLUMNS:
select *
from INFORMATION_SCHEMA.COLUMNS
where TABLE_NAME='tableName'
All information on the create parameters is stored in the system tables like TABLES.
Dumping information about a table:
exec sp_help 'tableName'
If you have netezza SQL Aginity Editor 4.1 or above, then the following helps:
Right click on the tablename in the Object Browser Pane
Hover over Script in the menu
Click on 'DDl query to new window'
This will give you the SQL code required to create the table.

SQL Developer Shortcuts

I am using SQL Developer to connect to an Oracle DB.
I would like to be able to see the constraints of a table via the command window. Usually I have to navigate the tables tree and then open the table details. Is there a shortcut to allow me to access the constraints (in particular the FK constraints) by writing a command? Ideally I want something like
desc table_name
where desc describes the table. I know desc is SQL based but are there any commands I can use within SQL developer?
Thanks in advance.
If you can browse for the table in table browser under your connection. Open it. You can find a constraints tab where you can find the information about constraints on the table. Instead you can click on table name in command window (the worksheet) press the shortcut Shift +F4 which will give the same information.
A similar question was asked here:
http://p2p.wrox.com/oracle/30730-sp_help-equivalent-oracle.html
Someone suggested:
select * from user_constraints where table_name=[yourtable]
There are other suggestions too...
The following select command will show the table details in Oracle
Select * from user_tables
where table_name='COUNTRIES'
Another solution to see the description of table in Oracle, on sql> prompt
SQL>Description Countries
or
SQL>Desc Countries
This will show the column name and datatype as well as constraints.

How to handle DROP TABLE if there is a view on a table?

View can be dropped using the DROP VIEW command. Is there a solution for handling DROP TABLE if there is a view on a table?
I tried like
DROP TABLE ORDER_TBL CASCADE;
Is there another way in a modern DBMS?
Per the MSDN documentation:
http://msdn.microsoft.com/en-us/library/ms173790.aspx
Any view or stored procedure that references the dropped table must be
explicitly dropped by using DROP VIEW or DROP PROCEDURE.
In other words, MSSQL doesn't automagically warn you, or drop the view for you. The view simply becomes invalid.
MSDN continues:
To report the dependencies on a table, use
sys.dm_sql_referencing_entities.
For example:
http://sqlserverdownanddirty.blogspot.com/2011/07/finding-object-dependancies-in-sql.html
SELECT referencing_schema_name, referencing_entity_name, is_schema_bound, [definition]
FROM sys.dm_sql_referencing_entities ('mySchema.myTable', 'OBJECT') r
INNER JOIN sys.sql_modules m
ON r.referencing_id = m.object_id
Option 1:
If you run this script before DROP TABLE, it lists all objects that have a reference on your table:
SELECT DISTINCT so.name,so.xtype
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%YOUR TABLE%'
If you want just views, you can filter the above query by AND s.xtype='V'.
Option 2 :
You can add WITH SCHEMABINDING to the definition of your view so you can not drop based table.
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
Using SQL Search before you drop your table, you can find out what other objects are relying on that table. You'll need to deal with those in some ways - remove a dependency or drop those objects as well along with your table.
I was trying to import a dump and the "user" table was already there. Drop table did not work:
ERROR 1965 (42S02): 'mysql.user' is a view
In MySQL you can do:
drop view user;
This removed the table and no "drop table" was necessary, solving my import problem.

helptext for create table

Can I get the whole query which I used for creating a table, like we have sp_helptext to get the query of a stored procedure.
sp_helptext 'procedure_name'
Is there anything like this available for create table also in SQL server express?
I want to view the whole query which I wrote for creating a particular table and not the table structure.
Like if a deleted a table, and again want to create it, then I would have to type the whole query again, so I want a way through which I don't have to write the whole query again, like in mysql there is an option such as SHOW, which shows the table query?
In SQL Server Management Studio you can right-click on a table in the Object Explorer window and choose to generate the CREATE script into a new query window or put it in the clipboard or save it in a file.
Try sp_columns or sp_help. But this will not give you the CREATE TABLE text, you have to create this text for yourself.
You can also have a look at Catalog Stored Procedures

a question about oracle sql developer?

i have created tables and views, and insrted some test data, but i want to know where i can get the sql code that i used to create and insert the data, i tried to look in the sql history, but its no thier, thanks :))
You can get the SQL required to create and populate the table:
1) For the create table DDL, go to the Tables node and select the table, then click on the SQL tab and copy the DDL it displays.
2) For the insert DML, right-click on the table name in the Tables node and select Export Data then Insert... and fill in the wizard. You can also get the create table DDL this way rather than as I said in step 1.
history F8 should show you everything that has happened in that tab within SQL Developer (for instance file xyz.sql).
if you cannot find the history or the ddl that you wish, you ought to be able to get the ddl 'simply' enough
by
select *
from addmas
where colA = 'my criteria'
;
then clicking F9, then right clicking onto the grid and export data to 'insert'
and that will create the insert statments for you (keep in mind to put in a table name)
to get the CREATE TABLE/VIEW (or object, not the alter tables) you may right click on the table name,
select 'edit' then go down to DDL (create). If you use that tool for your edits it'll also show you the ddl for the alters