Parameters in stored procedures in SQL Server - sql-server

How many types of parameters are there in a stored procedures and what are they?
Thanks in advance.
And can we delete a table using view? I think yes but in what situation we can't delete it if there are no trigger associated with that table. I mean to say i need to delete a table which has no trigger associated with it using view, in which case i can't delete it?

You have basically three types of parameters for stored procedures:
Input
Output
InputOutput
Is that what you're looking for??
Also, I don't totally understand what you're asking with your second question? You want to "delete a table" ?? You don't delete tables - you DROP tables. And you can't use a view to drop a table..... or do you mean: can you delete rows from a table through a view?
SQL Server views can indeed be used to modify data - to a certain degree, and by obeying a certain set of rules. Read more about that on MSDN under Modifying Data Through a View.

Related

Is it possible to us MSAccess to insert data through an external linked View in SQL Server? [duplicate]

This question already has answers here:
Any way to edit data on MS-Access form using SQL View with two tables
(3 answers)
Closed 2 years ago.
I'm using MS Access 2016 and SQL Server 2012.
I have single table with a schemabound view over the table. Our design was to allow our users to insert data through the view so we can do some id lookups for them and audit who inserted the record using an instead of insert trigger.
No matter how I configure it, Access doesn't want to let me add records to the view. If I use the table, it works as expected and it lets me insert new records.
I've tried the following:
Opening the external table and try to add new record at bottom - no luck
Creating an "Update" query - no luck
Creating an "Append" query - no luck
Creating a form, setting the view as a source, and setting Data Entry = Yes - no luck
Is what I'm trying to do even possible? I come from a SQL/C# background, and have only basic MSAccess skills. Any help is appreciated!
EDIT: I found out the View had a group by when it shouldn't have. I corrected that, but now it is complaining that it is not updatable because there are multiple underlying tables.
EDIT2: I've made progress but it's not ideal.
More info:
This view represents a datawarehouse fact, and joins to two dimension tables to get business keys.
I was able to get this to work by creating a "dummy" table with all the same fields, then creating a view over that. I was able to insert, but the data comes back "deleted" immediately because my instead of trigger fires.
Data went to the right place, but obviously the view doesn't show any data since it's not actually querying the correct tables anymore.
Clearly the view is irrelevant now, and I'll likely write the trigger over the dummy table.
It's not ideal, but I think my solution will be to have them insert through this dummy table, and then have a second view that does the joins so they can view the results.
I appreciate the detailed answer that was given, but unfortunately it didn't help this situation.
If the view is up-datable from SSMS, then it will be so in Access. So, before you try anything in Access, you have to first ensure that the view is up-datable in SSMS. So not all SQL views are up-datable.
You would do beyond well to test/check this in SSMS.
Once you done this, then you have to link the table from access. (and if you messed around with modify to the view, then delete the linked view in access and re-link). YOU MUST delete the access link and re-link.
And the linking process needs extra CARE. WHEN you link, you will be asked for a primary key for the view. Because a view can have multiple PK's (as a result of several tables), then Access can't know or guess. And far worse is that a SQL view does not in fact have a defined PK, and there is no command or means that Access can use to determine this. So you are PROMPTED during that link process. I note this issue, since if you are using some VBA re-link code, then if you re-link and change the database (or server), then during that re-link process, the PK setting you had will be lost. You ONLY get this prompt during a link of a new table - not a re-link. So, keep this important detail in mind.
You can after the fact (after linking a table) execute the following command to re-enable or "set" which column is to be the PK with this command in Access:
I in fact use this routine:
Sub createPK(strTable As String, strPK As String)
CurrentDb.Execute "CREATE UNIQUE INDEX " & strPK & _
" ON " & strTable & " (" & strPK & ") WITH PRIMARY"
End Sub
So, to set a PK for a linked view, then I use this:
Call createPK("dbo_tblHotels","ID")
As a FYI:
The above command DOES NOT create a index in access for that linked view, but is a MEANS to ONLY TELL Access what column to use for the PK. So, in this context, the create index command is not creating a index, but is the means/approach/how/process in which you can tell/set in Access what column is to be used for the PK view. As noted, you only need to do the above if you using code to re-link (or create) a table (view in this case).
So, if you using the Access UI, and you link to a view? Then Access will prompt/ask you to choose a column for the PK. You can as noted after linking use the above routine/command in Access to set which column is to be used as the PK if you missed the prompt, or as noted are using some VBA code to re-link.
A re-link (refresh) with the Access UI to the SAME database will preserve the PK setting. But if you change the connection string (database or server name), then the PK setting will be lost.
First, this is a really good answer to a similar question. Try adding an INSTEAD OF trigger to the view in SQL Server. In order to insert into a view the key columns must all be present and each table must be UNION'ed together. INSTEAD OF trigger can be made to do exactly as you wish.

SQL Server stored procedure get many parameters

I'm new to SQL Server and I wonder:
I have built some tables that display the documents that produced in my program.
I need to write a stored procedure that inserts a document to the tables with transaction.
I think to create a main procedure with transaction that get 2 DTU of table:
main and details
My question is:
is it valid to create many DTU of table, for example:
if in my DB their is 10 tables i need create 10 data type user of tables?
How can I use polymorphism in the procedure parameter ,so I can write a one procedure that get for example:
if i have 2 tables person and teachers that all teacher is person so my parameter will be alwase as person type but i allow send also teacher type ?
After I saw the XML type but its more slowly to use that and also it is more difficult
I wonder if SQL Server has other solutions to write to multiple tables with one transaction that would require fewer parameters?
Thanks for advance and hope that you will help me
It all depends on your front-end. If you have two different UI to capture header and details separately. You would need two separate stored procs, if not one stored proc would suffice.
User Defined table type can decrease the number of params to stored proc. I agree XML is complex and user defined table type is much easier to use.

How to perform cleaning operations on all tables in a database based on actions in a table

I am not sure how to word this, and it might be easy, but I have one table with a list of actions for several variables
Example of data:
Table=X
Variable=AccountNumber
Action=Set to blank (written in SQL code)
I want the code to go to X.accountnumber and perform action, and do this for all the table/variables combination in a particular database
I assume I will need dynamic SQL for this? I am just trying to figure out how to get started in doing a column by column search for each table and if the column matches, call the action from the action table
Any ideas?
I'd recommend using the undocumented stored procedure sp_msforeachtable.
See here for more info
Agree with #Jeffrey Van Laethem, more information here
sp_MSforeachtable

SQL - How to check if altering a table will break any stored procedures

Is there a nice way before I alter a table (e.g. remove a column), to see if that this will break any stored procedures?
I am trying to do this in MS SQL Server
Use the query here to search all stored procedures for the table and column name. You will probably still want to look at the code for each one you find to verify that it will or won't break.
you can use the following query to search for the table name in any stored procedures:
SELECT name
FROM sys.procedures
WHERE Object_definition(object_id) LIKE '%Your_Table_Name%'
I suggest you:
Make sure you have a separate environment (DEV)
Use the sample code from here to create a proc that confirms all objects in the database can be recompiled
How to Check all stored procedure is ok in sql server?
Use it - I can guarantee you will already have failing objects before you remove your column
Remove your column and use it again to see if more things broke
The more mature approach to this is to put your database into a database project and build that. But you can't do this until your database is valid.

Using change table tracking

I need to use the changed table tracking feature of sql server 2008. I have enabled this on many tables. Now i have to write a sync program to transfer this data to another location.
My problem is how do i get only those tables whose data has changed without having to loop through all the changed tables list and checking each of them?
Try sys.CHANGE_TRACKING_TABLES (documented on MSDN here).
You'll have to use OBJECT_NAME to get the table name from the first column.

Resources