Showing posts with label t-sql. Show all posts
Showing posts with label t-sql. Show all posts

Thursday, March 22, 2012

A very beginer question: error in creating table

I am new in T-SQL. I have just create a empty database and would like to create some table in it. So I type the following in SQL Query Analyzer:

CREATE TABLE mydatabasename.mytablename (

some code here
)
But when execute, it give me an error from the first line "Specified owner name 'mydatabasename' either does not exist or you do not have permission to use it."

when I remove mydatabasename it inserts the table in to the master database, not in my database.

How can I insert the table into my database? Should I use SQL Query Analyzer or use others program? I 've tried to use the command "create table" in enterprise manager but it immediately give another error, some what like "could not use empty column name ... "
Seem to be too simple question, but I am really appreciate your helpWhen you use the SQL Query Analyzer do you choose from the dropdown your newly created Database?|||CREATE TABLE mydatabasename.mytablename

should be

CREATE TABLE mydatabasename.dbo.mytablename

A fully qualified name is
servername.databasename.owner.object

You can't skip one like you're doing. You can omit them from left to right, but can't skip a part.|||Thanks all. I will try both, select the newly database and use databasename.dbo.tablename.

Infact, I use to think about using "databasename.dbo.tablename" but then think it 's ridiculous for a try. What a mistake. It's my because my poor English make me sometime not understand the full syntax.

Thank you.

Tuesday, March 20, 2012

A T-SQL CURSOR issue, need help

Hi there,
I have a very strange problem when using a t-sql cursor, appritiated if someone could explain why.

When executing the SQL string to fill the cursor, everything works fine, nothin wrong so far

But when executing the whole query (traversing the sql string result) I experience the following:

First the code for the cursor

USE pubs
GO

-- Declare the variables to store the values returned by FETCH.
DECLARE @.au_lname varchar(40), @.au_fname varchar(20)
DECLARE @.iCounter smallint

DECLARE authors_cursor CURSOR FOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE "B%"
ORDER BY au_lname, au_fname

SET @.iCounter = 1

OPEN authors_cursor

-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.

FETCH NEXT FROM authors_cursor
INTO @.au_lname, @.au_fname

-- Check @.@.FETCH_STATUS to see if there are any more rows to fetch.
WHILE @.@.FETCH_STATUS = 0
BEGIN

-- Concatenate and display the current values in the variables.
PRINT "No: " + CAST(@.iCounter AS varchar(3)) + " Author: " + @.au_fname + " " + @.au_lname

SET @.iCounter = iCounter + 1

-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @.au_lname, @.au_fname
END

CLOSE authors_cursor
DEALLOCATE authors_cursor
GO

As I said the SQL query to fill the cursor works fine, but the result from the whole query above is this!?

No: 1 Author: Abraham Bennet
No: 2 Author: Reginald Blotchet-Halls
No: 3 Author: Abraham Bennet
No: 4 Author: Reginald Blotchet-Halls

No: 6 Author: Reginald Blotchet-Halls
No: 7 Author: Abraham Bennet
No: 8 Author: Reginald Blotchet-Halls

No: 10 Author: Abraham Bennet
No: 11 Author: Reginald Blotchet-Halls
No: 12 Author: Abraham Bennet
No: 13 Author: Reginald Blotchet-Halls

It seams that the query omits som fetches, does anybody know why?

In the BOL it says that WHILE loop only executes if the latest fetches succeeds, but why shouldnt it, the SQL query is ok!?

appritiate some help,
thanx in advance.I ran your code. Could not re-produce the error. Send me the data too. I assume you have fixed some sytax errors in your code.|||It was easier than I thougt, the error I mean.

Yes, I to found some syntax error after submitting the post, but the error was that some fields in my rowset containded a null value, and when the code tried to print that value it failed, which meant that the specific row never showed up.

I fixed by setting the vaule to 0

Sunday, March 11, 2012

a SqlDataSource can execute two InsertCommand?

hi everyone

i have a SqlDataSource
and i want execute two T-Sql (two InsertCommand)
but not success
How did i do this?

Thanks

1 Protected Sub SqlDataSource1_Inserted(ByVal senderAs Object,ByVal eAs System.Web.UI.WebControls.SqlDataSourceStatusEventArgs)Handles SqlDataSource1.Inserted
2 Dim SDSTempAs SqlDataSource =Nothing
3 Try
4 SDSTemp =New SqlDataSource(CnStr,"")
5 SDSTemp.InsertCommandType = SqlDataSourceCommandType.Text
6 SDSTemp.InsertCommand ="INSERT INTO Table1 (id,t1,t2) VALUES (@.id,@.t1,@.t2)"
7 Dim idAs String = e.Command.Parameters("@.id").Value.ToString
8 SDSTemp.InsertParameters.Add("id", id)
9 SDSTemp.InsertParameters.Add("t1",CType(FormView1.FindControl("TextBox1"), TextBox).Text)
10 SDSTemp.InsertParameters.Add("t2",CType(FormView1.FindControl("TextBox2"), TextBox).Text)
11 SDSTemp.Insert()
12
13 SDSTemp.InsertCommand ="INSERT INTO Table2 (id) VALUES (@.id)"
14
15 SDSTemp.InsertParameters.Add("id", id)
16
17 SDSTemp.Insert()
18 Catch exAs Exception
19 Message.Text = ex.Message.ToString
20 End Try
21 End Sub
22

you can use the ItemCommand Event and place your code in there. Here is a reference you can use:http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.formview.itemcommand.aspx

The following link shows an quick example I made to help ya out some more also:http://forums.asp.net/thread/1692239.aspx

Hope this helps.