TSql Lookup function - tsql

I have a bunch of dimension tables that have unique ID and Name fields. I need a T-SQL function that returns an ID when passed a table name and a value for the name field.
I'm guessing the function would build a little query then execute it? Performance isn't an issue since this is a one time ETL thing.

Sounds like you want a scalar user defined function
There's a lot of other ways to do it though, and udf's certainly can have some perfomance issues.

Related

Transpose table with unknown amount of variables

I would like to transpose table a to table b without knowing exactly how many procedures there are. Is there a way to include a loop inside a query?
Thank you in advance!
So far I am just checking what the maximum amount of 'procedures' are, I put all the procedures in an array, and then query all elements from this array. However I would like a query that always works without first defining the maximum amount of procedures.

tsql Table-Valued Function Best Practices

I work for a public school system and have to create behavior reports for various things. I have about a dozen reports where the data is generated via stored procedures and pretty much retrieve the same base data. I'd like to create a Table-Valued Function so that each report is pulling the same base dataset to work with.
I have a student table that has columns such as schoolYear, Grade, schoolID, etc. The key is enrollmentID. I'm trying to determine which is more efficient, to return all the fields I'll be working with from the student table (schoolYear, grade, etc) or just return the key (enrollmentID) and then join it back with the student table in the query that is calling the function.
On one hand, it seems redundant to join the student table in both the function and the stored procedure that is calling the function. On the other hand, thousands of records are potentially returning and it seems like in the past I've noticed significant lag if I'm trying to return a lot of data from a function.
Is there a best-practice on how to best use table-valued functions? As always your help is greatly appreciated!

Changed property value when selecting in EF4

I need to change the value of a property when I query the database using EF4. I have a company code that gets returned and I need to translate it to another company code, if needed. So, there is a stored procedure that is used to do this currently. Here's the old select statement.
SELECT companyName, TranslateCompanyCode(companyCode) as newCompanyCode FROM companyTable where companyCode = 'AA';
TranslateCompanyCode is the stored proc that does the translation. I'd like to do this in my new code when needed. I think I might need to use a Model-Defined Function. Anyone know how I can do this?
For your scenario, I would use a JOIN. Model-defined functions are cool when you need to perform a quick function on a value (particularly without an additional query). From a performance standpoint, a JOIN will be faster and more efficient than trying to put the sub-query in a model-defined function - particularly if you are selecting more than 1 row at a time.
However, if you do still want to use Model defined functions, then this example should point you in the right direction as to how to run a query within the function. This implementation will also be more complex than just using a join but is an alternative.

Using table names as parameters in t-sql (eg from #tblname)

Is it possible to use the name of a table as a parameter in t-sql?
I want to insert data into a table, but I want one method in C# which has a parameter for the table.
Is this a good approach? I think if I have one form and I am choosing the table and fields to insert data into, I am essentially looking to write my own dynamic sql query built on the fly. This is another thing altogether which I am sure has its catches?
Thanks
Not directly. The only way to do this is through dynamic SQL - either EXEC or sp_ExecuteSQL. The latter has the advantage of query cache/re-use, and avoiding injection via parameters for the values - but you will have to concatenate the table-name itself into the query (you can't parameterise it), so be sure to white-list it against a list of known-good table names.

Execute statements for every record in a table

I have a temporary table (or, say, a function which returns a table of values).
I want to execute some statements for each record in the table.
Can this be done without using cursors?
I'm not opposed to cursors, but would like a more elegant syntax\way of doing it.
Something like this randomly made-up syntax:
for (select A,B from #temp) exec DoSomething A,B
I'm using Sql Server 2005.
I dont think what you want to to is that easy.
What i have found is that you can create a scalar function taking the arguments A and B and then from within the function execute an Extended Stored Procedure. This might achieve what you want to do, but it seems that this might make the code even more complex.
I think for readibility and maintainability, you should stick to the CURSOR implementation.
I would look into changing the stored proc so that it can work against a set of data rather than a single row input.
Would CROSS/OUTER APPLY do what you want if you need RBAR processing.
It's elegant, but depends on what processing you need to do