How can I get the view to display the current column definition? - sql-server

Using SQL Server Express 2008 & Management Studio.
I have a table & a view. At the time the View was created, the Table's FirstName column was varchar(50). Modify Table.Firstname to be nvarchar(50) and the View still indicates that the column is varchar(50).
How do I get the View to display the current column definition?
Here's what I've tried (that didn't work):
Click the Refresh button
Close Management Studio & re-open it.
Here's what did work:
Create a brand new view. The new view does display the current field definition
Note: This project has many views, so I'd prefer not to have to re-create all of them.

Run either sp_refreshview or the newer sp_refreshsqlmodule
The best solution is to use WITH SCHEMABINDING so this can't happen.
Quite common on SO, example here

Look up sys.sp_refreshsqlmodule in Books Online.

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.

How to add a new column from a stored proc to an existing SSRS 2012 report

I have an existing stored procedure that I modified to return one more column. I need to get that new column into the existing SSRS report. The report is simple but I want to avoid recreating the whole report if possible. Thank you.
In DataSet properties, go to the "Query" tab. Press the "Refresh Fields" button, and click OK. Save the report. When you go to the "Fields" tab of DataSet properties, the new column should appear.
(This is for Visual Studio 2013. For other versions, the UI may be slightly different, but the basic approach still applies.)
The easiest way to edit the current report, most places I have worked have some type of file control like TFS. Another option is to download the rdl file off the server and edit it.
You have the dataset collection created for this , Edit that ( Ctrl + L ) add the new column name >> Save and refresh

SQL Server changing data type from decimal(18,7) to decimal(23,15)

One of my fields has datatype decimal(18,7). I would like to change it to decimal(23,15). When I attempt it, get error message about "Saving changes not permitted.... require the following tables to be dropped and re-created.... can't be re-created or enable the option Preventing saving changes that rquire that table to be re-create"
Where do I change options to allow table to be dropped and re-created? I have nothing valuable in this table, I can easily drop the table manually and re-create it manually, but would rather have SQL Server do it for me.
Regards,
Well, as it says, it is in the options. Which are in the menu.
And in the documentation:
http://msdn.microsoft.com/en-us/library/bb895146.aspx
To change this option, on the Tools menu, click Options, expand
Designers, and then click Table and Database Designers. Select or
clear the Prevent saving changes that require the table to be
re-created check box.

Change A Database's Table's Name (SQL Server 2008)

I got a table named tblHello and I wanna rename it to Hello
Right click on the table and select rename in management studio
You can also use sp_rename:
sp_rename 'old_table_name', 'new_table_name'
I want to point out that table renaming is not as simple as just changing the name when you have queries written. You also need to change all references to the old name in every stored proc, view, function and dynamic sql code. This is not something to be taken on lighty in something that is already on production.
But #jonH has the answer for how to do it (you run that in the query window making sure you switch to the correct database first). Of course you have to have the right security permissions to rename objects.
If its a small project,
you can directly change it from User Interface.
In Server Explorer, right-click the table you want to rename and Open Table Definition.
Right-click the table in the Table Definition window and choose Properties from the shortcut menu.
In the field for the Name value in the Properties, type a new name for the table.
Save the table.
And its done.

How can I stop SQL Server Management Studio replacing 'SELECT *' with the column list?

SQL Server Mgmt Studio is driving me crazy.
If I create a view and SELECT '*' from a table, it's all OK and I can save the view.
Looking at the SQL for the view (eg.by scripting a CREATE) reveals that the 'SELECT *' really is saved to the view's SQL.
But as soon as I reopen the view using the GUI (right click > modify), SELECT * is replaced with a column list of all the columns in the table.
How can I stop Management Studio from doing this ? I want my 'SELECT *' to remain just that.
Perhaps it's just the difficulty of googling 'SELECT *' that prevented me from finding anything remotely relevant to this (i did put it in double quotes).
Please, I am highly experienced in Transact-SQL, so please DON'T give me a lecture on why I shouldn't be using SELECT *. I know all the pros and cons and I do use it at times. It's a language feature, and like all language features can be used for good or evil (I emphatically do NOT agree that it is never appropriate to use it).
Edit: I'm giving Marc the answer, since it seems it is not possible to turn this behaviour off. Problem is considered closed. I note that Enterprise Manager did no similar thing.
The workaround is to either edit SQL as text, or go to a product other than Managment Studio. Or constantly edit out the column list and replace the * every time you edit a view. Sigh.
When SQL Server Mgmt Studio creates a view, I assume they're expanding the * to the complete list of columns that are present in the underlying table(s) at that particular time exactly for this reason: what if one of the underlying tables changes? Do you want the new columns to just simply show up in every view that references that table?? Seriously???
I think Microsoft tries to impmenent the "element of least surprise" here - your view will contain those columns that are present at the time the view gets created - and it stays that way, unless you explicitly and knowingly change that.
I for one wouldn't want to have my views suddenly having more columns than before, when an underlying table changes..... do you??
And I don't think there's any setting in Mgmt Studio to turn this behavior off, sorry.
Don't use the GUI editor.
Instead use the T-SQL editor. You get this by selecting "Script View As" -> "ALTER to" -> "New Query Window" from the right-click menu.
Try either of these: they are alternatives to using the GUI and can be setup as snippets with keyboard shortcuts:
select view_definition
from information_schema.views
where table_name = 'viewname'
or
exec sp_helptext 'viewname'
The results will retain the "select *". (Tested)

Resources