Thanks,
MatthewHello Mulktide!
I encountered the same error. Did you manage to solve the problem?
Thanks.
pax
MatthewHello Mulktide!
I encountered the same error. Did you manage to solve the problem?
Thanks.
pax
Hi,
I'm trying to figure out how to pass a field name into a procedure so the procedure can selectively find out the value of different fields. The DB table I'm interested in has multiple bool fields in it and the field name is the parameter I want to pass into the procedure. With the example code I list below, there is only one row that exists in the table so the end result should be dependent upon the value in the field. The calling procedure is "attempting" to pass in the name of the field, and the called procedure should use the paramter passed in as the field to return in the table. If the field returned in the called procedure is true, the called procedure returns "0" to the calling procedure, otherwise it returns 1. The syntax I have doesn't seem to work in that the called procedure always returns true from the field and the calling procedure always gets 0 back from the called procedure.
In the calling procedure, I'm doing a select just to find out if a 0 or 1 was returned, but this will not be the final version of the code, its just for test purposes.
Any help on what I'm doing wrong greatly appriciated.
- Bruce
ALTER PROCEDURE [dbo].[caller]
AS
BEGIN
SET NOCOUNT ON;
DECLARE @.include int,
@.Committee bit
execute @.include = dbo.callee @.Committee
IF @.include = 0
BEGIN
select * from ProvCompensation
END
ELSE
BEGIN
select * from ProvCommittee
END
END
ALTER PROCEDURE [dbo].[callee]
(@.fName bit)
AS
BEGIN
DECLARE @.aBool bit
SET NOCOUNT ON;
select @.aBool = @.fName from ProvisionsPlanSum where PlanId = -99
IF @.aBool = 'true'
BEGIN
RETURN (0)
END
ELSE
BEGIN
RETURN (1)
END
END
Generally speaking, parameterizing a column (or table for that matter) is not considered the "right" way to build SQL Server objects. But, if you really need to, you can use dynamic sql, and just execute it like:
declare @.aBool bit
declare @.query nvarchar(max)
declare @.fName varchar(10)
set @.fName = 'someCol'
set @.query = 'select @.aBool = ' + @.fName + ' from ProvisionsPlanSum where PlanId = -99'
exec sp_executeSQL
@.query, N'@.aBool bit output', @.aBool= @.aBool output
select @.aBool
Not 100% sure if this is perfect. I do know that this will work, if you need an example:
declare @.objectId int
exec sp_executeSQL
N'select @.objectId = max(object_id) from sys.objects',
N'@.objectId int output', @.objectId=@.objectId output
select @.objectId
This will also work :)
declare @.aBool bit
declare @.query nvarchar(1000)
declare @.fName varchar(30)
set @.fName = '1; DROP TABLE Test12; --'
set @.query = 'select @.aBool = ' + @.fName + ' from ProvisionsPlanSum where PlanId = -99'
EXEC sp_executeSQL
@.query, N'@.aBool bit output', @.aBool= @.aBool OUTPUT
SELECT @.aBool
|||Hey guys,
Thanks for the help. I'll give it a go sometime today.
... worked like poop through a goose, thanks guys!
- Bruce
I'm copying MyDbTable to TempdbTable. There are 12 fields and I'm ignoring 11 of them when copying the one table to another with DTSWizard.
Once the TempdbTable is there, I'm adding 2 fields along with the one that is there already.
1) field1 int, Nullable, Default value ((3))
2) field2 tinyint, Nullable, Default value ((1))
The reason why I'm creating these two fields and giving them a value so they will match up when I import TempdbTable to MyDb2ndTable.
The problem I'm having is... field1 and field2 have a Null value intead of the value I'm giving them.
I'm new at this, so maybe the cause of the problem is obvious, but if anyone can help me I'd really appreciate it.
Thanks,
Bill
When you add the new columns, you MUST make them NOT NULL if you wish to set a DEFAULT value and assign that default value to the existing rows.
ALTER TABLE #MyTable
ADD MyNewColumn varchar(30) NOT NULL DEFAULT 'N/A'
Thanks for responding Arnie.
I Ran This...
ALTER TABLE #xlaANLsubscribers
ADD listid int NOT NULL DEFAULT '3'
ADD isconfirmed tinyint NOT NULL DEFAULT '1'
I Got this Error...
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near 'isconfirmed'.
When I added one field only I got something like...
The table doesn't exist or you don't have permission.
Thanks,
Bill
|||
ALTER TABLE #xlaANLsubscribers
ADD listid int NOT NULL DEFAULT '3',
isconfirmed tinyint NOT NULL DEFAULT '1'
(without the additional ADD)
Jens K. Suessmeyer.
http://www.sqlserver2005.de
|||Thanks Jens,
This it the error I'm getting when running that, and the table is definitely there.
Msg 4902, Level 16, State 1, Line 1
Cannot find the object "#xlaANLsubscribers" because it does not exist or you do not have permissions.
Thanks,
Bill
|||Was the #Temp table created by the same user in the same session/connection?|||Thanks Arnie,
I imported the records into the table of the database with DTSWizard authenicated in Windows. I then copied this table to Temp with the same authenication, but I've been running your script and doing the editing of the Temp table in SQL authenication. So I tried running your script and editing in Windows authenication and the same error happened. How that's not to confusing.
Thanks,
Bill
|||Unless you specify the ## as a prefix instead of a single #, the table is only available to the session where it was created in. Even other connected session might have no access to the table.Jens K. Suessmeyer
http://www.sqlserver2005.de
|||I'm getting the same error even when I specify ## as the prefix of the table.|||
Bill,
Does this code execute without error.
Code Snippet
SET NOCOUNT ON
CREATE TABLE #MyTable
( RowID int IDENTITY,
Name varchar(20)
)
INSERT INTO #MyTable VALUES ( 'Bill' )
SELECT *
FROM #MyTable
ALTER TABLE #MyTable
ADD Address varchar(30) NOT NULL DEFAULT 'N/A'
SELECT *
FROM #MyTable
DROP TABLE #MyTable
Arnie,
No errors, it worked perfectly.
|||Then I assume that your executing user is another one than the one which was uses to execute Arnies script, right ? Try to create explicitly dbo.#Something and select also with the full qualified name.Jens K. Suessmeyer
http://www.sqlserver2005.de
|||
I can only suggest walking back throught the rest of your code (all code that touches the #xlaANLsubscribers table), and verify that there isn't a DELETE TABLE stuck in there somewhere...
If you are creating the #Temp table, and populating it in the same session, it 'should' be there. So I'm baffled at the moment and will have to give this some thought...
|||Jens,
I thought that schema/owner was ignored in TempDb...
|||I'm using DTSWizard to copy the table from the Main Database to Temp... am I wrong in doing it that way?With only a date stored in a DateTime, the time would be 00:00:00.000|||I created two fields in a sql table, one for storing date and one for time. In my VB, I populated them with #3/8/2005# and #10:00:00 PM# respectively. The program crashed with message "sqldattime overflow. Must be between 1/1/1753 12:00:00 AM ...." Why did the program crash? Should I combine two fields into one?|||Show some code.|||In my aspx form I created one text field and one drop down list. The value in the text field is pulled from MS calendar control so it is like 3/10/2005 and the ddl is filled with time in every 15 min. e.g. 10:00, 10:15.
In my vb code, I coded as following:
...
reservation.requesteddate=cdate(txtbkdate.text)
reservation.requestedtime=ddlRequestedTime.SelectedItem.Text
...
reservation.insert()
Should I combine two fields into only one? Thanks.|||DateTime fields ALWAYS contains a date and a time. You can use code to split them for independent display, if you wish. I certainly would combine them in a single field if they refer to the same event.|||Thanks, Doug. Yes, they do refer to the same event. What I intended to do is a field for booked date and another for booked time. I though by splitting into two fields, when I create tree view for booking for a particular date will be comparatively easier. But still, apart from such reason, if I do have a need to only store a time value in a datatime field, what is the proper way?|||The reality is, you ALWAYS store both, and then use client side (in this case, ASP.NET) formatting to show the data you want. Alternately, you can use SQL code to get just the date or time (see CONVERT() in SQL Server Books Online).|||Doug, here is another real situation I have, can I have your comments.
I have a table to store my restaurant's info and I need to store the business hours. I created two fields to store from and to. Before I create a form to maintain the info of this table, I simply input data from vs2003 and it did accept my entry (only 10:00 entered to a smalldatetime field). Now if I create a form for data entry, apparently it makes no sense to ask user to input date and time, so I have to do something in the vb code. But which is a more sensible way to do so? Should I just concatenate any date with the time entered by user and update it to sql table? How do other developers normally do? Since I'm developer from other platform, may be my though is totally unlogically...|||I would likely concatentate the date and time, then parse it into a single DateTime (I generally do not use SmallDateTime's). The question is, is the date and time a single attribute? Meaning, are you talking about 3/13/2005 10:15 AM as a single thing, so that at some point, you might want to find a row based upon that date/time combination.|||Sorry I may have confused you. Put it in a simply way, what I need to store is just only a time, the date is meaningless to me. So really I don't need to store a date to the database but according to what you said, the date portion is required when popuplating the database. It means that I have to put in a meaningless date even I don't need it. The way I see is if documentation is not done properly or the field is not named meaningfully then other programmers may be misled by the date when examining data. I hope this is clear for you to understand my point.
I'm trying this code but nothing is being displayed
SqlConnection conn =newSqlConnection(ConfigurationManager.AppSettings["STRING_CON"]);SqlDataAdapter da =newSqlDataAdapter("my_sproc", conn);
DataTable dt =newDataTable();DataRow dr;
//Adding the columns to the datatable
dt.Columns.Add("CategoryIdIn");dt.Columns.Add("CategoryNameVc");
foreach (DataGridItem itemin gd_freq.Items){
dr = dt.NewRow();
dr[0] = item.Cells[0].Text;
dr[1] = item.Cells[1].Text;
dt.Rows.Add(dr);
}//ForEach
da.Fill(dt);
gd_freq.DataSource = dt;
gd_freq.DataBind();
I am not clear what you are trying to do here.
Do you want to populate the datagrid by binding it to the datatable returned by my_sproc?
"fetch the value from each cell into datatable"
Where did the values in the cells come from?
Can you explain a bit more?
|||Hi Robert,
I'm also a little lost after reading your posted code. It seems that first you filled a table by fetching the data from a data grid and then you called a stored procedure and updated that table with data returned. Note here that all the data you got from the data grid will be overridden now. So, what exactly are you going to do?
If you just want to fetch the data from a data grid and then update the data source, you can try to use da.Update(dt) in your code (instead of da.Fill);
And if you want to fill the data grid with data from the database, first, you can filldt by executing da.Fill(dt), then you can set gd_freq.datasource=dt. Finally call gd_freq.databind and by now you will have your data grid well populated.
Hope my suggestion could help
Thanks.