I can use this sql to get object_id, table name and column name.
select a.object_id,a.name,b.name as fieldName
from sys.tables a
join sys.columns b on a.object_id=b.object_id
order by 1,2,3
It seems that the object_id is the unique identifier of table. But when I change the structure of table, then object_id will be changed. And I don't know which is the unique identifier of column?
Are there unique identifier which never changed for table and column? Thanks a lot!
#SMor Thanks for your reminding. I want to monitor the change history of
database structure information,including table name, field name and data type of field etc. And then we can analyze the change history for project summary or other usages.
Related
For example, if I query like so
SELECT TOP 100 * FROM sys.dm_fts_index_keywords_by_document(DB_ID, TABLE_ID)
I can get both keywords and document_id's. I want to go from document_id to the record involved. There has to be some view that correlates the document_id to my primary key?
I think I found the answer at Get Started With Full-Text Search:
Note: When your full-text key is an integer type (which is our recommendation), document_id maps directly to this value in the base table. However, if the full-text key is a non-integer type, document_id will not represent the full-text key in the base table. To identify the row in the base table returned by this dynamic management view (DMV), you must join it with the results returned by sys.sp_fulltext_keymappings. You must store this stored procedure output in a temporary table before you can join the document_id column with the DocId column returned by the stored procedure. For more information on this stored procedure, see the "sys.sp_fulltext_keymappings" topic in SQL Server Books Online.
I have 2 dimension tables DimLegalEntity and DimMember. They are not connected with any foreign key. Both the tables has "name" column which contains name of the institution. I would like to get the result which contains matching name column from both the tables.
PS. Some data may contain abbreviation in DimLegalEntity.
you can inner join both the table , for join u dont need primary foreign key relationship , as long as column value involved in join condition can be converted to one data type
I am trying to link 2 table in sql server let's assume one of them called customer and the other is product.
customer has custID as primary key and i want to link it with custID in product table as foreign key, and its give me this error:
The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_product_customer". The conflict occurred in database "xyz", table "dbo.customer", column 'custID #'.
Is there anyone know, how can I fix the issue?
This could have many reasons.
Do you have a type missmatch between those columns?
Is every customer in products also in your customer table?
You can check the second part by using this:
SELECT DISTINCT p.customer_id
FROM products as p
LEFT JOIN customers as c ON p.customer_id = c.customer_id
WHERE c.customer_id is null
Hope this give you a hint.
I import data into a SQL Server database from Excel.
The data is inserted into a tmptable which has no primary key defined.
tmptable:
course, trainer, jockey, horse, won, pos, date
I want to copy the data from tmptable into another table mastertable which has a primary key column.
mastertable:
coursefk, trainerfk, jockeyfk, horsefk, won, pos, date.
The columns suffixed 'fk' refer to individual other tables.
My hope is that during the insert into mastertable, the script would take the value of course column from tmptable and refer to the table Courses for the appropriate value to be placed into mastertable. The primary and foreign key relationships between the tables have been created, but I cannot create a script that will achieve what I want.
Can anyone please help.
You can join tmptable with other foreign key tables.
Something like this.
INSERT INTO mastertable(coursefk, trainerfk, jockeyfk, horsefk, won, pos, date)
SELECT mastercourse.coursefk, mastertrainer.trainerfk, masterjockey.jockeyfk, masterhorse.horsefk, won, pos, date
FROM tmptable
LEFT JOIN mastercourse ON mastercourse.course = tmptable.course
LEFT JOIN mastertrainer ON mastertrainer.trainer = tmptable.trainer
LEFT JOIN masterjockey ON masterjockey.jockey = tmptable.jockey
LEFT JOIN masterhorse ON masterhorse.horse = tmptable.horse
Note: Since I do not know structure of other FK tables, Please replace and use appropriate table and column names.
I have added a new column in table, when I select this table with schema name, new column shows, when i select this column without mention of schema name, SQL does not identify new column, so basically:
Select new_column From schema.table works
Select new_column from table does not work
Please tell me why this is happening & how to correct this, i want this column available without mentioning schema name.
If you are not getting new column when you are selecting from table whithout schema name, then this means that that schema name is not default and you have 2 tables with that name both in default and mensioned schema. You can have tables with same name in different schemas.
It turned out that column is not getting selected because I am querying a view, not a table, and I need to add this column to view as well.
Confusion occurred because in previous versions of product(Dynamics AX) they(Microsoft) did not apply schema to database objects, now they added schema & created a view for backward compatibility.