In Dataverse if I create a table e.g 'foo' MS Dynamics automatically pre-fixes my tables with a few characters to denote that they are my custom tables e.g. abcd_foo.
What's more, if connect to the database with SSMS I can query the table as follows:
Select * from abcd_foo;
When you create a Choice column in foo, the table contains an integer value that references the lookup value - not the lookup value itself.
The picture below shows the choice values returned from a query:
Select
abcd_accesslevel -- my choice column
from abcd_foo;
What is the name of the SQL Table in which MS Dynamics stores these 'choice values' which it then internally joins up to my tables foo choice column, acceslevel?
e.g. if 'abcd_accesslevel' is a Choice column, how would I complete this query:
Select f.abcd_accesslevel,
c.label, -- The choice label
c.value, -- The choice value
f.*
from abcd_foo f
left join some_internal_choice_table c on c.someid = f.abcd_accesslevel
-- There may be a more complex WHERE clause to separate out the label for my table from the other choice values if all choices are stored in a common table.
In Dataverse there are 2 optionsets, local and global.
In your case I believe your abcd_accesslevel is local optionset.
local optionset is stored in metadata Entitydefinitions.
I never retrieved this via sql but I user API to fetch such data.
example API call
https://XYZ.crm.dynamics.com/api/data/v9.2/EntityDefinitions(LogicalName=’incident’)/Attributes/Microsoft.Dynamics.CRM.PicklistAttributeMetadata?&$expand=OptionSet
I tried this sql query and worked for me. (I tried on my account entity)
SELECT *
FROM stringmap
WHERE objecttypecode = 'abcd_foo'
AND attributename = 'abcd_accesslevel ';
Related
Result is fetching zero records.
enter image description here
Are you connected to (logged into) the database in which that table exists?
SQL such as this would be a starting point
select * from _v_relation_column
where UPPER(name) = 'MY_TABLE'
and UPPER(schema) = 'MY_SCHEMA'
order by attnum;
The restriction on schema may not be needed (if you aren't using multiple schemas).
(A) Sometimes we need the column names as headers
select * from <table> limit 0
(B) If you want definition of a table ie. showing all columns along with their data types etc (assuming you have nzsql cli)
nzsql <db> -c "\d <table>"
I am learning how to use SQL server recently. I do not understand why use master..sysdatabases and test..sysobjects in the following statements:
select name from [master]..[sysdatabases] where dbid=1;
select count(1) from [test]..[sysobjects] where xtype = 'U';
What does the 1 in count(1) mean? Does it mean the first column?
Thanks for any helpful answers.
Your first line basically gets the name of the master database (it looks at the list of all databases, and returns the name of the database with the ID of 1, which in this case is generally going to be 'master').
Do a to see all the databases on a server:
SELECT * FROM [master]..[sysdatabases]
Note that the row with "dbid" = 1, is the row for the "master" database, which is a system database present on all SQL Server instances.
Your second line counts the number of rows in the sysobjects collection in the database named 'test' where the type is a user table (i.e. not a stored procedure, not a system table, etc).
In the expression "[x]..[y]", the 'x' is the name of the database, and 'y' is the name of the table or view within that database.
If you had a database named "Foo", and in there was a table named "Bar", then this statement would return the count of rows in that table:
SELECT COUNT(1) FROM [Foo]..[Bar]
As Ed Gibbs above described, the '1' is just a place-holder for counting the total number of rows in the most efficient way possible on any possible database or version. It's become a sort of short-hand way of counting.
I am currently using IN clause on a varchar field. Will using Contains of FTS help in performance?
For e.g.
Select * from Orders where City IN (‘London’ , ‘New York’)
vs
Select * from Orders where Contains (City, ‘London or New York’)
Thanks in advance.
Table Definition
CREATE TABLE Orders(ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),City VARCHAR(100))
GO
INSERT INTO Orders
VALUES ('London'),('Newyork'),('Paris'),('Manchester')
,('Liverpool'),('Sheffield'),('Bolton')
GO
Create FTS on City Column using ID as the key
Used SSMS to create FTS Index.
Queries
-- Query 1
Select * from Orders
where City IN ('London' , 'NewYork')
GO
-- Query 2
Select * from Orders where
Contains (City, '"London" or "NewYork"')
GO
Execution Plans for both queries
As you can see The Query which used FTS costed 3 times more than the query which used IN Operator.
Having said this, when it comes to find Language specific terms in sql server FTS is the way to go, for example looking for Inflectional forms , Synonymous and much more Read Here for more information.
When query a Derby database, I find out that for some tables I have to double quote the column name and use table name to qualify the column name, but for some other tables I don’t need to. What happens to these tables and how can I make all tables the same and can query them without the double quote and the table name qualifier? I am using NetBeans IDE’s Sql Command tool. Below are those different queries.
Set schema app;
Select * from table1 where table1.”state” = ‘CA’;
Select * from table2 where state = ‘CA’;
Putting a tablename or column name in quotes, sometimes referred to by the jargon-y term "delimited identifiers" does two things:
Allows you to use words that are otherwise reserved keywords (e.g., naming a column "WHERE" or "SELECT")
Instructs the database system to process the name using case sensitive rules, rather than case-insensitive rules
So if you originally created "table3" with a CREATE TABLE statement that specified "table3" in double quotes like this, then you will forever after have to refer to it with the name in double quotes.
select * from table3
will be automatically processed by the database as if it was
select * from TABLE3
while
select * from "table3"
will successfully match the table you created as create table "table3"
See: http://db.apache.org/derby/docs/10.9/ref/crefsqlj34834.html
My application is continuously polling the database. For optimization purpose, I want the application to query the database only if the tables have been modified. So I want to calculate the HASH of entire table and compare it with the last-saved-hash of table. (I plan to compute the hash by first calculating HASH of each row and then followed by their hash i.e. HASH of HASHes)
I found that there is Checksum() sql utility function for SQL Server which computes HASH/Checksum for one row.
Is there any similar utility/query to find the HASH of a row in SQL Anywhere 11 database?
FYI, the database table does not have any coloumn with the precomputed HASH/Checksum.
Got the answer. We can compute the hash on a particular column of a table using below query:
-- SELECT HASH(coulum_name, hash_algorithm)
-- For example:
SELECT HASH(coulmn, 'md5')
FROM MyTable
This creates a hash over all data of a table, to detect changes in any column:
CREATE VARIABLE #tabledata LONG VARCHAR;
UNLOAD TABLE MyTable INTO VARIABLE #tabledata ORDER ON QUOTES OFF COMPRESSED;
SET #tabledata = Hash(#tabledata);
IF (#tabledata <> '40407ede9683bcfb46bc25151139f62c') THEN
SELECT #tabledata AS hash;
SELECT * FROM MyTable;
ENDIF;
DROP VARIABLE #tabledata;
Of course this is expensive and shouldn't be used if the data is hundreds of megabytes. But if the only other way is comparing all the data for any changes, this will be faster and produces load and memory consumption only on the db server.
If the change detection is only needed for a few columns, you can use UNLOAD SELECT col FROM table INTO ... instead.