I have multiple tables that use a scalar function in a column's check constraint. Some tables use the same function in more than one column. These constrains obviously all have different names.
I need to alter this function, however first I must remove all constraints that reference it.
Is it possible to get a list of tables and and columns that references this function as a constraint, based on the functions name?
In an ideal world, the name of the constraints would all contain the he name of the function they reference, however unfortunately this is not the case with my database.
you can use this script :
select s.name as constraintName,c.name as ColName,o.name as TableName
from sys.check_constraints s
join sys.all_columns c on s.parent_object_id=c.object_id and s.parent_column_id=c.column_id
join sys.objects o on o.object_id=c.object_id
where s.definition like '%your function name%'
Related
I have a table name with spaces between say like 'Orders Details'.
I want to use that table in stored procedures with joins.I have tried using alias names,[],`` in queries but nothing seems to be working.Can anybody help me on this error!
Do not use order as a column alias. It is a SQL keyword. I would just use o:
select o.*, od.*
from orders o join
order_details od
on o.orderid = od.orderid
where year(o.orderdate) = #orderyear;
Notes:
Your JOIN condition is on ProductId. However, that is highly suspicious. Usually such joins are on the order id. In fact, ProductId doesn't belong in Orders (usually) if there is a detail table.
Do not define your tables with spaces in the name. That just makes it hard to reference the names.
orderdate does not seem to be defined, because it has a red underline.
I don't recommend select *. For one thing, you will have duplicate column names. More importantly, you want to be explicit about what this code returns, particularly in a stored function or procedure.
I ran the following query
SELECT sm.object_id,
v1.object_name,
o.type,
o.type_desc,
sm.definition
FROM sys.sql_modules sm
CROSS APPLY (VALUES (OBJECT_NAME(sm.object_id))) v1 (object_name)
JOIN sys.objects o ON sm.object_id = o.object_id;
And there are three objects with a wrong relation between object_name and definition. There is no match, no correspondence between the name and the definition it references.
It looks like this tables didn't track the delete or changes in name and definitions of these three objects.
How can this situation can be given?
How can I "update" this tables or fix this properly?
This is a side effect of using "sp_rename".
These objects will work fine, but to refresh their definitions you need to recreate them.
From sp_rename documentation:
Renaming a stored procedure, function, view, or trigger will not
change the name of the corresponding object either in the definition
column of the sys.sql_modules catalog view or obtained using the
OBJECT_DEFINITION built-in function. Therefore, we recommend that
sp_rename not be used to rename these object types. Instead, drop and
re-create the object with its new name.
I am an extreme rookie when it comes to SQL and I am trying to self teach myself. I have a few questions regarding SQL and writing queries:
I've been given some examples of queries by a colleague and many of them have the field names beginning with either m. or t. or o.email (eg. m.email or t.email or o.email). What do the prefixes indicate?
I am attempting to write a JOIN query but keep receiving an error message saying: An expression of non-boolean type specified in a context where a condition is expected, near ')' What would cause this? Neither of the data extensions I am trying to join contain boonlean fields.
I've written it as:
SELECT DISTINCT email, status_type, status_value_text FROM ent.[Table 1] JOIN [Table 2] ON email
Again, I am extremely new, any help would be greatly appreciated!
The general form of an SQL SELECT query is:
SELECT (columns, expressions, aggregate functions)
FROM (data sources)
WHERE (filters on data sources)
GROUP BY (columns used to group the data, needed if you use aggregate functions)
HAVING (filters on data AFTER it's grouped)
About the FROM clause, if you use more than one data source (table or view), you should think how your data will be related:
A cross join returns the cartesian product of the tables involved.Example:
FROM foo, bar will return all the rows of table foo and all the rows from table bar, without any rule on how they are related.
An inner join returns only the data that fulfills a relation rule.
Example:
FROM foo INNER JOIN bar ON foo.aField = bar.aField (alternative: FROM foo JOIN bar ON foo.aField = b.aField) will return only the rows of foo and bar that fulfils the condition provided (the field aField in each table must match). Important: An INNER JOIN must have a boolean expression, i.e. the relation must either be true or false; most times it will be an equality relationship (=) but it can be anything that returns a TRUE/FALSE value (>, <, >=, etcetera).
An outer join returns the full data of one table and only the rows of the other table that match the relation rule (for any other non-matching row of the first table, the columns from the second table will have a NULL value).
There are two possible outer joins: LEFT JOIN will return all the rows from the table on the left of the relation rule, and only the matching rows of the right table of the relation. RIGHT JOIN does just the opposite.
Example:
FROM foo LEFT JOIN bar ON foo.aField = bar.aField
As it was the case with the INNER JOIN, the relation must have a boolean expression.
Now, as you've noticed, you need to tell the system from which field you're getting the data. That's the reason for the "prefixes": They are the table (or schema.database.table) names. If you want, you can use aliases on this table names (just as you can use aliases on fields):
Example: FROM foo AS f INNER JOIN bar AS b on f.aField = b.aField
The aliases used in the FROM clause must be used every time you use the field in the same SELECT query.
Now, talking explicitly about your query: The JOIN in your post is missing the relation rule: You're telling the database server that the tables are related, but you're not defining the relation rule. Wich fields must match? Complete the JOIN expression with the columns that must match between the tables.
What do the prefixes indicate?
If you look at the FROM clause of the example queries you are questioning, you should notice table aliases specified after the full table names. Aliases are used sometimes just to shorten the table names from needing to be fully spelled out, but most often they are needed to disambiguate between similarly named columns in more than one of the tables in the FROM.
SELECT
-- Get the email column from the table aliased
-- as `m` (table1)
m.email
FROM
-- table1 aliased as m
table1 AS m
-- table1 aliased as e
INNER JOIN table2 AS e ON....
Consider two related tables, table1 and table2. Both of them has a column called email. In the SELECT list, you may not specify merely email because the RDBMS won't know which one you want. Instead, you must qualify it with the table name. Since the tables were aliased as m, e in the FROM, you must use the alias in SELECT instead of the full table name.
MS SQL Server documentation on table aliases...
An expression of non-boolean type specified in a context where a condition is expected, near ')'
In your join's ON clause, you simply supplied the column name email, which we can assume is a common value relating the two tables. A join's ON clause expects a boolean expression wherein a true value doing a row-wise comparison between the tables results in a row being returned.
So the ON clause needs a boolean expression with two sides, or something returning TRUE. In your case, it is equality between the email columns
SELECT DISTINCT
-- Must qualify email since both tables have it
-- Using the full table name, or its alias if an alias
-- was provided in `FROM`.
[Table 1].email,
status_type,
status_value_text
FROM
ent.[Table 1]
-- Equality between email columns completes the join
JOIN [Table 2] ON [Table 1].email = [Table 2].email
The expression in ON can be anything which evaluates to TRUE. It need not be an exact match between column values, though an exact match is by far the most common use case. You could say for example ON 1 = 1, which is always true. The resultant rowset would match every row from [Table 1] to every row of [Table 2] (which is a cartesian product). It could also be ON 1 = 2 which is never true, and therefore basically pointless since it would never return rows.
Using a syntax similar to the ON email you attempted, some RDBMS systems support a USING() in place of ON, allowing you to specify the equal column instead of a boolean expression. You might therefore also express it as
FROM
ent.[Table 1]
JOIN [Table 2] USING (email)
See also What's the difference between ON and USING
The m. or t. are aliases for tables within your query (so you are probably using tables that begin with the letter m and t). The alias is specified after the table name in the FROM part of your query.
The syntax looks off for your JOIN. The query should probably look more similar to this:
SELECT DISTINCT t1.email, t1.status_type, t2.status_value_text
FROM
[Table 1] t1 JOIN [Table 2] t2 ON t1.email = t2.email
Check out this link to see more examples.http://www.w3schools.com/sql/sql_join.asp
I have a stored procedure that references a few tables. However, it refers to the tables with letters.
So let's say a column called Name is from the table Users, then the stored procedure may call the column name U.Users.
My question is, how do I get a list of all such mappings i.e all the letters that map to a table?
you are referring to table aliases, each distinct query can have their own "mapping". These alias values are not specific to stored procedures. Here is an example:
select
a.col1, a.col2
FROM YourTable1 a
select
b.col1, b.col2
from YourTable2 a
inner join YourTable1 b on a.col1=b.col2
YourTable1.col1 and YourTable1.col2 are returned in both of the above queries, although they have the "a" alias in the first query and "b" alias in the second query. See this Using Table Aliases.
In examples like above, people often use a table alias because is is quicker to write a.col1 than YourTable.col1. There is no way for anyone to know the aliases used in your stored procedure, you need to figure that out, look at these examples to help:
FROM YourTable a
-- ^table ^alias
FROM YourTable AS a
-- ^table ^alias
FROM YourTable1 a
INNER JOIN YourTable2 b ON a.col1=b.col1
-- ^table ^alias
FROM YourTable1 AS a
INNER JOIN YourTable2 AS b ON a.col1=b.col1
-- ^table ^alias
Each statement assigns its own aliases for the tables it uses, if at all. The same table used in different statements could be aliased differently (or not aliased). There cannot possibly be a single place to look up every table alias or a simple method to recover them from all the statements in your stored procedure or function or view etc.
I have two tables CountryMaster and StatesMaster. The fields are:
CountryMaster(CountryId, Name)
StateMaster(StateId, Name, CountryId)
The StateMaster.CountryId is a Foreign key. I want to get the Name of States from StateMaster as well as the Name of the Country to which that State belongs from CountryMaster.
I want this in one query.
How can i get this?
SELECT
s.Name AS StateName
, c.Name AS CountryName
FROM
dbo.StateMaster s
INNER JOIN
dbo.CountryMaster c
ON c.CountryId = s.CountryId
Join Fundamentals
By using joins, you can retrieve data from two or more tables based on
logical relationships between the tables. Joins indicate how Microsoft
SQL Server should use data from one table to select the rows in
another table.
A join condition defines the way two tables are related in a query by:
Specifying the column from each table to be used for the join. A
typical join condition specifies a foreign key from one table and its
associated key in the other table.
Specifying a logical operator (for example, = or <>,) to be used
in comparing values from the columns.
Inner joins can be specified in either the FROM or WHERE clauses.
Outer joins can be specified in the FROM clause only. The join
conditions combine with the WHERE and HAVING search conditions to
control the rows that are selected from the base tables referenced in
the FROM clause.