Specify name of table types in SQL Server [duplicate] - sql

This question already has an answer here:
SQL Server create User-Defined Table Types with schema not working properly
(1 answer)
Closed 9 years ago.
Create a new database in MS SQL Server 2008 R2 and then create a new table type with the following command
CREATE TYPE typeMyType AS TABLE (ID INT)
Now execute the following query
SELECT OBJECT_NAME (object_id) AS ObjectName, *
FROM sys.indexes
WHERE index_id <= 1
ORDER BY ObjectName
This will show you that an object of type HEAP was created which is fine as the data for typeMyType has to be stored somewhere.
But the object is called TT_typeMyType_01142BA1 in my case.
Question:
Why isn't it called typeMyType and how can I overwrite this obviously server generated name?

The table type is listed in several places that can be looked at via the following system tables (sys.sysobjects, sys.indexes, sys.table_types and sys.types).
I can understand that table_types is a subset of types. Therefore, those two places are where to look for my table type AssociativeArray in my AdventureWorks2012 database.
The question I have is why is it looking like a table in sysobjects and sysindexes?
We have not defined any use for the variable yet. However, it looks like a table. It must be how the engine defines meta data for future use.
One take away, does the information in sysobjects and sysindexes get updated a run-time when we declare a variable of type AssociativeArray?
Also, what happens when two SPIDS create the same variable at the same time with different data being inserted?
That is a in-depth engine question that maybe a someone from Microsoft CAT team might know off the top of their head.
I guess I could do some research to find out.
Enclosed is a link stating that table variables use tempdb. That is what I know is a fact. Again, I wonder if the sys.objects and sys.indexes get updated or just place holders.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html

Related

Using SSMA to convert from Access to SQL, scripting the fixes

I am using SSMA to convert from an Access db to a SQL 2019 DB.
There are some things I need to fix in the access DB so I am trying to figure out whether or not these things can be done via a query in access or you have to use the goofy UI and do everything manually.
So I had a couple of questions about queries in Microsoft Access:
Can you modify the 'required' attribute on a column within a table by using a query?
Can you configure Index (dupes) on a column by using a query?
Can you change validation rules using a query?
Can you create/delete relationships using a query?
Can you change the field length of a column by using a query?
Any examples of any of these would be helpful, when I google for ms access related things all of the content is either related to Access 2007/2010 or its very UI heavy rather than Query heavy.
I am trying to script this because I may have to do this migration several times.
Update: I was able to get most of what i needed figured out..
ALTER TABLE Users ALTER COLUMN Type CHECK(In ("I","U","") Or Is Null);
Still havent found a way to change the 'ValidationRule'.. trying to change it to
In ("I","U","") Or Is Null
Look into the Data Definition Language section of the MS Access SQL Reference, specifically the ALTER TABLE statement, which will cover the majority of your questions.
For example, in response to:
Can you change the field length of a column by using a query?
ALTER TABLE Table1 ALTER COLUMN Field1 TEXT(100)
The above will change the data type of the field Field1 within table Table1 to a text field accommodating 100 characters.

Copy all columns without data but with dependencies in SQL [duplicate]

This question already has answers here:
In SQL Server, how do I generate a CREATE TABLE statement for a given table?
(16 answers)
Closed 5 years ago.
So i'm trying to copy all data of table CarOrders into a new table, NewCarOrders. But i only want to copy columns and dependencies and not data. I don't know if it truly matters, but i'm using SQL Server Management Studio.
Here are two things that i tried, but none worked.
SELECT *
INTO NewCarOrders
FROM CarOrders
WHERE 0 = 1
The issue with above is, it is copying all the columns but it is not copying the dependencies/relationships.
I saw some posts on some of the forums regarding this, but the suggested solution was using SSMS Wizard. I want to do it using SQL (or T-SQL).
I'm new to SQL world, so i'm really sorry if it is a stupid or no brainer question. Let me know if i am missing some important information, i'll update it.
Try below query and check if this works for you
SELECT TOP 0 *
INTO NewCarOrders
FROM CarOrders
This will create NewCarOrders table with same structure as CarOrders table and no rows in NewCarOrders.
SELECT * FROM NewCarOrders -- Returns zero rows
Note : This will not copy constraints , only structure is copied.
For constraints do as below -
In SSMS right click on the table CarOrders, Script Table As > Create To > New Query Window.
Change the name in the generated script to NewCarOrders and execute.
Also change the constraints name in the generated script else it will throw error like There is already an object named 'xyz' in the database
You can use the like command:
CREATE TABLE NewCarOrders LIKE CarOrders;

Describe is not working in IBM DB2

I am running an query in IBM DB2 as;
DESCRIBE TABLE Schema.Table
But I am getting an error as
DESCRIBE TABLE Schema.Table Error 42601:Token Table was not valid. Valid tokens: :. SQLCODE=-104
I search a lot but can't find out the reason and as I am very new in IBM DB2 so can't figure out the matter. Is it a permission related issue?
I don't have command prompt access.
You appear to be using DB2 on IBM i (formerly known as AS/400), where catalog views are in the QSYS2 schema.
In recent versions there are also their equivalents: SYSIBM.SQLCOLUMNS and INFORMATION_SCHEMA.COLUMNS.
If you are simply trying to get catalog information for a table or view, the system catalog will work just fine, as noted in another answer by mustaccio. But if you want to embed a DESCRIBE TABLE in your RPG or COBOL program, that will work as well. One reason you might want to do this is if you have a dynamic number of columns, or you don't know the table name at compile time. You can use an sql descriptor built by describing a table or cursor to receive the output of a FETCH statement in your program. You will need an SQL Descriptor or an SQLDA to receive the description of the table. It would look something like this:
dcl-s tableName Varchar(128);
exec sql allocate sql descriptor 'D1' with max 20;
tableName = 'MYTABLE';
exec sql
describe table :tableName
using sql descriptor 'D1';
This will retrieve information about the table into the specified descriptor. In this case D1. The descriptor name can be a host variable. This example allocates a local descriptor for 20 items. If your table has more than 20 columns, you can request a larger descriptor in the ALLOCATE DESCRIPTOR statement. If you will be spreading your sql that uses a given descriptor across multiple modules, you will need to use a global descriptor by replacing 'D1' with global 'D1'. You can also use an SQLDA, but I find that those can be more difficult to work with.
To get information out of the descriptor you would use GET DESCRIPTOR. It would be beyond the scope of this site to go into all the details of what you can get out of the descriptor, but as an example you could get the column name of the first column of MYTABLE like this:
dcl-s columnName Varchar(128) Inz('');
exec sql
get sql descriptor 'D1'
value 1 :columnName = name;
Don't forget to deallocate the descriptor when you are through with it.
exec sql deallocate sql descriptor 'D1';
You can find more information on DESCRIBE TABLE here https://www.ibm.com/support/knowledgecenter/ssw_ibm_i_71/db2/rbafzdescrtb.htm. The knowledge center also has information about ALLOCATE DESCRIPTOR, DEALLOCATE DESCRIPTOR, and GET DESCRIPTOR.

How to check if existing database matches program's format

First, I'm new to Java DB programming so maybe my approach is wrong. So please keep that in mind. Now the question:
My program let's the user open an existing Java Derby database. When it's opened I want to check if the database contains the correct tables and each table has the correct columns - not just names but type also. So far I'm trying to create each table and if it throws an exception then I know the table exists. Now I need to check column types. What is the best practice to do this? Hard-code check for each column type as answered here in the catch block?
If you want to figure out what tables are present in the database, and what columns each table has, etc., there are (at least) two simple approaches:
Issue SQL queries against the Derby system catalogs, such as
select s.schemaname || '.' || t.tablename
from sys.systables t, sys.sysschemas s
where t.schemaid = s.schemaid
and t.tabletype = 'T'
order by s.schemaname, t.tablename;
Use the API provided by java.sql.DatabaseMetaData, and its methods such as getSchemas(), getTables(), and getColumns()

SQL Server 2008: copy table structure, and schema

thanks for your time. i edited my script, ran it, and still got this name: srp.dbo.gstDataCutover. i used to be able to do this easily with MSSQL2005. we've recently upgraded to 2008. and i dont remember doing it any other way...
Hi,
I'm trying to copy a table structure (columns, datatypes, schema) into a new table to have the same schema and structure, using the sql code below.
SELECT dbo.gstData.*
INTO [dbo.gstDataCutover]
FROM dbo.gstData
WHERE dbo.gstData.gstID < 1
My problem is, when i run this script the new table dbo.gstDataCutover is named as "dbo.gstDataCutover" but the schema is defaulted to the system schema ("srp"), which is actually srp.[dbo.gstDataCutover].
I want to copy both the structure and the schema.
Thanks!
Without any periods, the hard brackets indicate table name -- it's including the "dbo." in your example as part of the table name.
If you want the table created in the dbo schema:
SELECT t.*
INTO dbo.gstDataCutover
FROM dbo.gstData t
WHERE t.gstID < 1
Likewise, if you want the table created in the srp schema:
SELECT t.*
INTO srp.gstDataCutover
FROM dbo.gstData t
WHERE t.gstID < 1
The table name doesn't have any unusual characters, so there's no need to use hard brackets...
You can download the community edition of Visual Studio, which has features for comparing schemas as well as data. It will list the differences and allows you to select a set of changes, for which it will generate an update-script.