Showing posts with label example. Show all posts
Showing posts with label example. Show all posts

Wednesday, March 28, 2012

question about query part of a string

I'm passing a variable to SQL and I want it to query a column (IP_user), but query any part of what is given. For example if I given Chris it would bring up Chris, Christian, Christine, etc. What is the syntax to do this? thanks

Try this

ALTER PROCEDURE [dbo].[SPU_XXXX]

@.SEARCH VARCHAR(30) = NULL

AS

SET @.SEARCH = '%'+ @.SEARCH + '%'

SELECT CLIENT.CLIENT, CLIENT.CLIENTID

FROM CLIENT

WHERE (CLIENT.CLIENT LIKE @.SEARCH)

|||

The best reference for this question is here:

http://www.sommarskog.se/arrays-in-sql.html

The Table of Numbers method is the easiest, fastest, etc. It will put the data into a table format that you can join to your table in a very straightforward fashion.

http://www.sommarskog.se/arrays-in-sql-2005.html#tblnum

|||

Code Snippet

Set @.search = 'Chris'

--To get all names start with Chris

--i.e., Chrisxxxxxxxx

select * from yourtable where ip_user like @.search + '%'

--To get all names ends with Chris

--i.e., xxxxxChris

select * from yourtable where ip_user like '%' + @.search

--To get all names containes Chris at anyplace

--i.e., xxxxxxChrisxxxxxxxx or xxxxxChris or Chrisxxxxxxxx

select * from yourtable where ip_user like '%' + @.search + '%'

|||

You really should look into Fulltext, as that is its strong feature. Another option using sql engine is to use charindex() function.

e.g.

select *

from tb

where charindex(@.var,your_column)>0

|||

Thank you that was very helpful and it worked great. I have another quick question, and although this isn't technically an access forum it involves access. I have someone who runs access and with an odbc connection, connects to the sql server database. Is there any sql that can be used in accesses queries to replicate this? I apologize ahead of time for posting this here, I couldn't find an access forum...

|||In Access, mark the Query as a 'Pass Though' query, and the query as above will be sent to execute on SQL Server without Access attempting to check syntax and compile.

Monday, March 26, 2012

question about primary key

how can I set my primary key to star again from 1' for example, if I had a
table with some rows in it and I deleted all that rows, next time when I
insert a new row in that table, primary key starts from last primary key of
previous rows, but I want it to start again from 1.
is that possible'
thanks!Bernard,
presumably you mean that your primary key has an identity attribute? In this
case if you truncate the table the seed is reset, or you can use DBCC
CHECKIDENT :
USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED)
GO
to get the original seed value (usually 1)
or
USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED, 30)
GO
to set the initial value to be 30.
HTH,
Paul Ibison

question about primary key

how can I set my primary key to star again from 1' for example, if I had a
table with some rows in it and I deleted all that rows, next time when I
insert a new row in that table, primary key starts from last primary key of
previous rows, but I want it to start again from 1.
is that possible'
thanks!Bernard,
presumably you mean that your primary key has an identity attribute? In this
case if you truncate the table the seed is reset, or you can use DBCC
CHECKIDENT :
USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED)
GO
to get the original seed value (usually 1)
or
USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED, 30)
GO
to set the initial value to be 30.
HTH,
Paul Ibisonsql

question about primary key

how can I set my primary key to star again from 1? for example, if I had a
table with some rows in it and I deleted all that rows, next time when I
insert a new row in that table, primary key starts from last primary key of
previous rows, but I want it to start again from 1.
is that possible?
thanks!
Bernard,
presumably you mean that your primary key has an identity attribute? In this
case if you truncate the table the seed is reset, or you can use DBCC
CHECKIDENT :
USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED)
GO
to get the original seed value (usually 1)
or
USE pubs
GO
DBCC CHECKIDENT (jobs, RESEED, 30)
GO
to set the initial value to be 30.
HTH,
Paul Ibison

Question about paging?

Good Day!

i have a problem regarding on paging.

in my Report i want to view the record 1 record per page.

Example:

I have a textboxes where i view my data the

- Firstname

- organization

-Department

I want it to view it 1 page per records.

How to do that? Sorry coz im a new bie in the reporting services

can anybody help me?

I got the already the paging per records but the problem is there is a blank page in next page. Example page 1 has a records then page 2 is a blank page then page 3 has a record?

Why is that? what is the cause?

Canany body help me? Your help is greatly appreciated.

|||

Hey,

Is it ok if you view like

First Name Organization Department

Then use table, to see the records and create a group and give your field like first name , department. and check page break at the end so it gives you in 1 record per page.

Any doubts let me know.

Amarnath

|||Make sure that there are no report items after the group that you have set to break.

Friday, March 23, 2012

question about like

Hello, i'm just wondering if i have for example a table with 2 colummns the first one is some primary key (int) and the other is of the image data type.
For example one row would be:

key data
232 0x7A5B6789D4E2...(thats just how sql server 2005 express shows me the data when i make a query insede Management Studio Express)

now i'm wondering if it is possible to do query for lets say %89D4% with LIKE,(i kinda want to search for a certain pattern of bits or in this case hexadecimal values ) i've tryed but i get an error. I just want to know if it is possible to do this with like and how and if it isn't, what other options do i have (full-text search - ?)?Well, the problem is that you are looking at binary data, not string data... Also, know that the image datatype is deprecated and should be replaced with varbinary(max).

What was the error you received?|||i tryed the following sql statement:
select * from DOCU_hlp.dbo.HTML a1 where a1.ID=100009 AND a1.HTML LIKE 0x%89D4% ;

and got the error message:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'D4'.

however this statement: select * from DOCU_hlp.dbo.HTML a1 where a1.ID=100009 AND a1.HTML LIKE 0x89D4 ;
does not give me any errors, but this statement doesn't really help any, since it searches for the exact value 0x89D4.

and about the part that image is depricated, well the database that i have to use already exists, and is full of data so i'm not to sure that i have anything to say about the data types that they use, just have to go with what they have ...|||

FitzFarseer wrote:

i tryed the following sql statement:
select * from DOCU_hlp.dbo.HTML a1 where a1.ID=100009 AND a1.HTML LIKE 0x%89D4% ;

and got the error message:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'D4'.

however this statement: select * from DOCU_hlp.dbo.HTML a1 where a1.ID=100009 AND a1.HTML LIKE 0x89D4 ;
does not give me any errors, but this statement doesn't really help any, since it searches for the exact value 0x89D4.

and about the part that image is depricated, well the database that i have to use already exists, and is full of data so i'm not to sure that i have anything to say about the data types that they use, just have to go with what they have ...

You need to surround your like statements with quotes...

select * from table where field like '%xxx%'|||select * from DOCU_hlp.dbo.HTML a1 where a1.ID=100009 AND a1.HTML LIKE '%89D4%' ;

gives me the following error:
Msg 8116, Level 16, State 1, Line 1
Argument data type varchar is invalid for argument 2 of like function.|||For your reading pleasure, and for reference:

MSDN Transact-SQL Reference for: LIKE

http://msdn2.microsoft.com/en-us/library/ms179859.aspx|||

FitzFarseer wrote:

select * from DOCU_hlp.dbo.HTML a1 where a1.ID=100009 AND a1.HTML LIKE '%89D4%' ;

gives me the following error:
Msg 8116, Level 16, State 1, Line 1
Argument data type varchar is invalid for argument 2 of like function.

I think this is a result of trying to use like on a binary column. Syntactically, it is correct.sql

Wednesday, March 21, 2012

Question about JOIN syntax

what is the syntax to join a table with the result of antoher query.
For example i have two tables
Create Table Customers (
CustomerID int,
LastPaymentDate Datetime )
Create Table Payments (
PaymentID int,
CustomerID int,
PaymentDate Datetime )
What query will bring me the customers whose lastpaymentdate in the
customers table is not correct.
That can only be checked by comparing it with the max paymentdate for each
customer in the payments table.
I want this to be done by ansi standard sql. Not using any specific feature
of sql server.
thxSELECT *
FROM Customers AS C
WHERE lastpaymentdate <>
(SELECT MAX(paymentdate)
FROM Payments AS P
WHERE P.customerid=C.customerid)
--
David Portas
--
Please reply only to the newsgroup
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:2vydnRekZvROvmiiRVn-sw@.giganews.com...
> SELECT *
> FROM Customers AS C
> WHERE lastpaymentdate <>
> (SELECT MAX(paymentdate)
> FROM Payments AS P
> WHERE P.customerid=C.customerid)
>
oh may be my example was not the best!
what if I have
Create Table Customers (
CustomerID int,
LastPaymentAmount int,
LastPaymentDate Datetime )
Create Table Payments (
PaymentID int,
CustomerID int,
Amount int,
PaymentDate Datetime )
and I want to see if the LastPaymentAmount field holds correct values.
I couldnt use the max function for this obviously.
thx|||> I couldnt use the max function for this obviously.
Why not? My query should still work.
If you want a query that uses a join then try this:
SELECT C.*
FROM Customers AS C
LEFT JOIN
(SELECT customerid, MAX(paymentdate) AS lastpaymentdate
FROM Payments
GROUP BY customerid) AS P
ON C.customerid=P.customerid
AND C.lastpaymentdate=P.lastpaymentdate
WHERE C.lastpaymentdate IS NOT NULL
AND P.customerid IS NULL
This has a possible advantage over the correlated subquery version. It will
include Customers who have a Lastpaymentdate but don't have any non-NULL
dates in the Payments table.
--
David Portas
--
Please reply only to the newsgroup
--|||Oops, you wanted the *correct* values:
SELECT C.*
FROM Customers AS C
JOIN
(SELECT customerid, MAX(paymentdate) AS lastpaymentdate
FROM Payments
GROUP BY customerid) AS P
ON C.customerid=P.customerid
AND C.lastpaymentdate=P.lastpaymentdate
--
David Portas
--
Please reply only to the newsgroup
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:4eOdnfkpJ63Bqmii4p2dnA@.giganews.com...
> Oops, you wanted the *correct* values:
> SELECT C.*
> FROM Customers AS C
> JOIN
> (SELECT customerid, MAX(paymentdate) AS lastpaymentdate
> FROM Payments
> GROUP BY customerid) AS P
> ON C.customerid=P.customerid
> AND C.lastpaymentdate=P.lastpaymentdate
> --
> David Portas
> --
> Please reply only to the newsgroup
> --
I want the LastPaymentAmount of Customers table to be compaired with the
Amount of the last payment in Payments table.
Ofcourse the last payment is the one with the maximum PaymentDate but the
Amount fields should be compared in both tables to see if they are equal.|||You didn't include any keys or constraints with your DDL but I'll have to
assume that (customerid, paymentdate) is unique in Payments - otherwise how
will you define which payment is the latest?
SELECT C.customerid, C.lastpaymentamount, C.lastpaymentdate
FROM
(SELECT customerid,
MAX(paymentdate) AS paymentdate
FROM Payments
GROUP BY customerid) AS P1
JOIN Payments AS P2
ON P1.customerid = P2.customerid
AND P1.paymentdate = P2.paymentdate
JOIN Customers AS C
ON P2.customerid = C.customerid
AND P2.paymentdate = C.lastpaymentdate
AND P2.amount = C.lastpaymentamount
--
David Portas
--
Please reply only to the newsgroup
--|||>> what query will bring me the customers whose last_payment_date in
the
customers table is not correct. <<
Your design is wrong; this is a computed value and you do not store
computed values in an RDBMS. Your Customers table should not exist at
all! And you do not have keys or anything that makes payments into a
table. Let's get the basics right:
CREATE TABLE Payments
(payment_id INTEGER NOT NULL PRIMARY KEY,
customer_id INTEGER NOT NULL
REFERENCES Customers
ON DELETE CASCADE
ON UPDATE CASCADE,
payment_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);
Now create a VIEW which will always be correct:
CREATE VIEW CurrentPayments (payment_id, customer_id, payment_date)
AS
SELECT payment_id, customer_id, payment_date
FROM Payments AS P1
WHERE P1.payment_date
= (SELECT MAX(payment_date)
FROM Payments AS P2
WHERE P1.customer_id = P2.customer_id);|||--CELKO-- (joe.celko@.northface.edu) writes:
> Your design is wrong; this is a computed value and you do not store
> computed values in an RDBMS.
Maybe in some small ideal model of a database you don't.
In real-world databases, you often find that you can save a lot of
cycles by storing computed data. For instance, if you have a transactions
table for a bank account, you probably want to store the balance on the
account after each transaction, because you will have several functions
in you system where you display this information. Having it computed
means that this function will run faster, and that the likelyhood that
they will show the wrong value because of bugs is less.
The one problem with storing computed values, is that if an underlying
value changes, the computed value changes too. But in the example I
just gave, this never happens due to business rules.
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se
Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinfo/productdoc/2000/books.asp|||>> this is a computed value and you do not store computed values in an
RDBMS. <<
May I know why so? Snapshots and stored joins, as recommended by the
relational model are typical examples where computed values are stored in an
RDBMS.
In SQL Server, you can materialize computed columns and views by physically
clustering them and I don't see why this should not be done, when required.
--
- Anith
( Please reply to newsgroups only )

Question about JOIN syntax

what is the syntax to join a table with the result of antoher query.

For example i have two tables

Create Table Customers (
CustomerID int,
LastPaymentDate Datetime )

Create Table Payments (
PaymentID int,
CustomerID int,
PaymentDate Datetime )

What query will bring me the customers whose lastpaymentdate in the
customers table is not correct.

That can only be checked by comparing it with the max paymentdate for each
customer in the payments table.

I want this to be done by ansi standard sql. Not using any specific feature
of sql server.

thxSELECT *
FROM Customers AS C
WHERE lastpaymentdate <>
(SELECT MAX(paymentdate)
FROM Payments AS P
WHERE P.customerid=C.customerid)

--
David Portas
----
Please reply only to the newsgroup
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:2vydnRekZvROvmiiRVn-sw@.giganews.com...
> SELECT *
> FROM Customers AS C
> WHERE lastpaymentdate <>
> (SELECT MAX(paymentdate)
> FROM Payments AS P
> WHERE P.customerid=C.customerid)

oh may be my example was not the best!

what if I have

Create Table Customers (
CustomerID int,
LastPaymentAmount int,
LastPaymentDate Datetime )

Create Table Payments (
PaymentID int,
CustomerID int,
Amount int,
PaymentDate Datetime )

and I want to see if the LastPaymentAmount field holds correct values.
I couldnt use the max function for this obviously.

thx|||> I couldnt use the max function for this obviously.

Why not? My query should still work.

If you want a query that uses a join then try this:

SELECT C.*
FROM Customers AS C
LEFT JOIN
(SELECT customerid, MAX(paymentdate) AS lastpaymentdate
FROM Payments
GROUP BY customerid) AS P
ON C.customerid=P.customerid
AND C.lastpaymentdate=P.lastpaymentdate
WHERE C.lastpaymentdate IS NOT NULL
AND P.customerid IS NULL

This has a possible advantage over the correlated subquery version. It will
include Customers who have a Lastpaymentdate but don't have any non-NULL
dates in the Payments table.

--
David Portas
----
Please reply only to the newsgroup
--|||Oops, you wanted the *correct* values:

SELECT C.*
FROM Customers AS C
JOIN
(SELECT customerid, MAX(paymentdate) AS lastpaymentdate
FROM Payments
GROUP BY customerid) AS P
ON C.customerid=P.customerid
AND C.lastpaymentdate=P.lastpaymentdate

--
David Portas
----
Please reply only to the newsgroup
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:4eOdnfkpJ63Bqmii4p2dnA@.giganews.com...
> Oops, you wanted the *correct* values:
> SELECT C.*
> FROM Customers AS C
> JOIN
> (SELECT customerid, MAX(paymentdate) AS lastpaymentdate
> FROM Payments
> GROUP BY customerid) AS P
> ON C.customerid=P.customerid
> AND C.lastpaymentdate=P.lastpaymentdate
> --
> David Portas
> ----
> Please reply only to the newsgroup
> --

I want the LastPaymentAmount of Customers table to be compaired with the
Amount of the last payment in Payments table.
Ofcourse the last payment is the one with the maximum PaymentDate but the
Amount fields should be compared in both tables to see if they are equal.|||You didn't include any keys or constraints with your DDL but I'll have to
assume that (customerid, paymentdate) is unique in Payments - otherwise how
will you define which payment is the latest?

SELECT C.customerid, C.lastpaymentamount, C.lastpaymentdate
FROM
(SELECT customerid,
MAX(paymentdate) AS paymentdate
FROM Payments
GROUP BY customerid) AS P1
JOIN Payments AS P2
ON P1.customerid = P2.customerid
AND P1.paymentdate = P2.paymentdate
JOIN Customers AS C
ON P2.customerid = C.customerid
AND P2.paymentdate = C.lastpaymentdate
AND P2.amount = C.lastpaymentamount

--
David Portas
----
Please reply only to the newsgroup
--|||>> what query will bring me the customers whose last_payment_date in
the
customers table is not correct. <<

Your design is wrong; this is a computed value and you do not store
computed values in an RDBMS. Your Customers table should not exist at
all! And you do not have keys or anything that makes payments into a
table. Let's get the basics right:

CREATE TABLE Payments
(payment_id INTEGER NOT NULL PRIMARY KEY,
customer_id INTEGER NOT NULL
REFERENCES Customers
ON DELETE CASCADE
ON UPDATE CASCADE,
payment_date DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL);

Now create a VIEW which will always be correct:

CREATE VIEW CurrentPayments (payment_id, customer_id, payment_date)
AS
SELECT payment_id, customer_id, payment_date
FROM Payments AS P1
WHERE P1.payment_date
= (SELECT MAX(payment_date)
FROM Payments AS P2
WHERE P1.customer_id = P2.customer_id);|||--CELKO-- (joe.celko@.northface.edu) writes:
> Your design is wrong; this is a computed value and you do not store
> computed values in an RDBMS.

Maybe in some small ideal model of a database you don't.

In real-world databases, you often find that you can save a lot of
cycles by storing computed data. For instance, if you have a transactions
table for a bank account, you probably want to store the balance on the
account after each transaction, because you will have several functions
in you system where you display this information. Having it computed
means that this function will run faster, and that the likelyhood that
they will show the wrong value because of bugs is less.

The one problem with storing computed values, is that if an underlying
value changes, the computed value changes too. But in the example I
just gave, this never happens due to business rules.

--
Erland Sommarskog, SQL Server MVP, sommar@.algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||>> In real-world databases, you often find that you can save a lot of
cycles by storing computed data. <<

OLAP versus versus OLTP databases. What I found is that the summary
data is usually processed by many applications at the same time (i.e.
end of the month, end of the quarter, end of the year), so you can
materialize the VIEWs and share them at that point in time instead of
always updating a redundant permanent table.

Unfortunately, SQL Server is not designed with that kind of sharing in
mind; it is more of a mainframe, heavy batch processing system approach.

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!|||>> this is a computed value and you do not store computed values in an
RDBMS. <<

May I know why so? Snapshots and stored joins, as recommended by the
relational model are typical examples where computed values are stored in an
RDBMS.

In SQL Server, you can materialize computed columns and views by physically
clustering them and I don't see why this should not be done, when required.

--
- Anith
( Please reply to newsgroups only )|||I agree with Joe. (Not that he needs my agreement!) Your customers
table should hold information that describes each customer. The
customer's payment history doesn't belong there. You can have a view
that holds each customer's latest payment if that's something you use
a lot. And why do you have a table for payments? You should have a
table for transactions, of which payments are only one flavor.

On Fri, 2 Jan 2004 13:45:29 +0500, "Mahmood" <a29zitkitwe@.yahoo.com>
wrote:

>"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
>news:2vydnRekZvROvmiiRVn-sw@.giganews.com...
>> SELECT *
>> FROM Customers AS C
>> WHERE lastpaymentdate <>
>> (SELECT MAX(paymentdate)
>> FROM Payments AS P
>> WHERE P.customerid=C.customerid)
>>
>oh may be my example was not the best!
>what if I have
>Create Table Customers (
>CustomerID int,
>LastPaymentAmount int,
>LastPaymentDate Datetime )
>
>Create Table Payments (
>PaymentID int,
>CustomerID int,
>Amount int,
>PaymentDate Datetime )
>and I want to see if the LastPaymentAmount field holds correct values.
>I couldnt use the max function for this obviously.
>thx
>

Monday, March 12, 2012

Question about dynamic report

Hi:

I have a question of create a dynamic the report.

For example: At the beginning I have 3 columns in the report, build by VS. After couple weeks, the same report needs to have more columns. And i don't want to use the VS to modify the layout manually, instead, when adding new columns, I can call a function to modify the rdl file add attributes into the xml file automatically. But after that which way I can go to publish the modified report into the report server, then when user clicks on that report again, there will be more columns show up.

thanks

LuZ,

You can use the Reporting Services web service to publish the new report. See this link for documentation:

http://msdn2.microsoft.com/en-us/library/ms154697.aspx

-Ryan

Question about dynamic report

Hi:

I have a question of create a dynamic the report.

For example: At the beginning I have 3 columns in the report, build by VS. After couple weeks, the same report needs to have more columns. And i don't want to use the VS to modify the layout manually, instead, when adding new columns, I can call a function to modify the rdl file add attributes into the xml file automatically. But after that which way I can go to publish the modified report into the report server, then when user clicks on that report again, there will be more columns show up.

thanks

LuZ,

You can use the Reporting Services web service to publish the new report. See this link for documentation:

http://msdn2.microsoft.com/en-us/library/ms154697.aspx

-Ryan

Wednesday, March 7, 2012

Question about a SQL Reporting Tutorial Example

In the BOL "Walkthrough - Using a Dynamic Query in a Report" it says:
In Data view, select the Employees dataset, and then use the generic
query designer to replace the original query with the following
expression:
="SELECT FirstName, LastName, Title FROM Employee" &
IIf(Parameters!Department.Value = 0,""," WHERE (DepartmentID = " &
Parameters!Department.Value & ")") & " ORDER BY LastName"
This expression
If I put that in the generic query designer it will not validate the
SQL.
Can anyone help with this?Expression-based queries cannot be validated and executed in the generic
query designer, because it does not evaluate the VB.NET expressions. They
are only evaluated at report runtime.
The general recommendation for dynamic queries using VB.NET expressions is
to start with a static query, then add a textbox to the report with the
dynamic query expression you want to use. Run the report to verify that the
expression-based commandtext evaluates to valid SQL, and then in the last
step replace the static query with the dynamic expression-based query. In
addition, the fields list is only auto-detected from non expression-based
queries.
--
This posting is provided "AS IS" with no warranties, and confers no rights.
"Chris" <cmay@.walshgroup.com> wrote in message
news:44f6e099.0409051123.191910b6@.posting.google.com...
> In the BOL "Walkthrough - Using a Dynamic Query in a Report" it says:
> In Data view, select the Employees dataset, and then use the generic
> query designer to replace the original query with the following
> expression:
> ="SELECT FirstName, LastName, Title FROM Employee" &
> IIf(Parameters!Department.Value = 0,""," WHERE (DepartmentID = " &
> Parameters!Department.Value & ")") & " ORDER BY LastName"
> This expression
> If I put that in the generic query designer it will not validate the
> SQL.
> Can anyone help with this?

Saturday, February 25, 2012

question abot primary key

how can I set my primary key to star again from 1?? for example, if I had a
table with some rows in it and I deleted all that rows, next time when I
insert a new row in that table, primary key starts from last primary key of
previous rows, but I want it to start again from 1.

is that possible??

thanks!I think you are talking about an IDENTITY column, which is not the same as a
Primary Key (although IDENTITY is used as a surrogate key).

DBCC CHECKIDENT ('TableName', RESEED,0)

--
David Portas
SQL Server MVP
--|||>> how can I set my primary key to start again from 1? <<

Please read a book on RDBMS; you have absolutely no idea what you are
doing.

Let's get back to the basics of an RDBMS. Rows are not records; fields
are not columns; tables are not files; there is no sequential access
or ordering in an RDBMS, so "first", "next" and "last" are totally
meaningless.

A primary key **BY DEFINITION** is a subset of attributes (columns).
Keys do not "start" anywhere. Each entity in the data model has a
unique primary key by its nature; it is not assigned by the PHYSICAL
storage used to hold the PHYSICAL representation of the DBMS.

If you had a 1950's magnetic tape file, then your question would make
sense. They start at record #1 each time you hang a new magnetic tape
on the drive, which is obviously your mental model. This is the
difference between a VIN number and a parking space number.

Question

I got a maintenance plan that does a checkdb everyday at
1:00 pm (for example). If i add a job to this maintenance
plan (going to job, adding a step to the job that
exectutes the maintenance plan..) it works fien but when
i open my maintenance plan to alter it (ex: add a
database) i get an error and all my setting are erased.
If i reconfigure it, it creates ANOTHER job using the
previous name and adding a 1 to it.. anyone know why this
is happeneing and how can i work arround this?!?!?!?
Don't modify the jobs created by maint wiz. Main wiz is a simple to use tool for the most basic installations.
If you need customizations beyond maint wiz, create your own jobs (or use CmdExec jobsteps to execute
sqlmaint.exe).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"?" <anonymous@.discussions.microsoft.com> wrote in message news:1c53c01c452bd$3c2bda60$a301280a@.phx.gbl...
> I got a maintenance plan that does a checkdb everyday at
> 1:00 pm (for example). If i add a job to this maintenance
> plan (going to job, adding a step to the job that
> exectutes the maintenance plan..) it works fien but when
> i open my maintenance plan to alter it (ex: add a
> database) i get an error and all my setting are erased.
> If i reconfigure it, it creates ANOTHER job using the
> previous name and adding a 1 to it.. anyone know why this
> is happeneing and how can i work arround this?!?!?!?

Monday, February 20, 2012

Question

I got a maintenance plan that does a checkdb everyday at
1:00 pm (for example). If i add a job to this maintenance
plan (going to job, adding a step to the job that
exectutes the maintenance plan..) it works fien but when
i open my maintenance plan to alter it (ex: add a
database) i get an error and all my setting are erased.
If i reconfigure it, it creates ANOTHER job using the
previous name and adding a 1 to it.. anyone know why this
is happeneing and how can i work arround this?!?!?!?Don't modify the jobs created by maint wiz. Main wiz is a simple to use tool for the most basic installations.
If you need customizations beyond maint wiz, create your own jobs (or use CmdExec jobsteps to execute
sqlmaint.exe).
--
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"'" <anonymous@.discussions.microsoft.com> wrote in message news:1c53c01c452bd$3c2bda60$a301280a@.phx.gbl...
> I got a maintenance plan that does a checkdb everyday at
> 1:00 pm (for example). If i add a job to this maintenance
> plan (going to job, adding a step to the job that
> exectutes the maintenance plan..) it works fien but when
> i open my maintenance plan to alter it (ex: add a
> database) i get an error and all my setting are erased.
> If i reconfigure it, it creates ANOTHER job using the
> previous name and adding a 1 to it.. anyone know why this
> is happeneing and how can i work arround this?!?!?!?

Question

I got a maintenance plan that does a checkdb everyday at
1:00 pm (for example). If i add a job to this maintenance
plan (going to job, adding a step to the job that
exectutes the maintenance plan..) it works fien but when
i open my maintenance plan to alter it (ex: add a
database) i get an error and all my setting are erased.
If i reconfigure it, it creates ANOTHER job using the
previous name and adding a 1 to it.. anyone know why this
is happeneing and how can i work arround this?!?!?!?Don't modify the jobs created by maint wiz. Main wiz is a simple to use tool
for the most basic installations.
If you need customizations beyond maint wiz, create your own jobs (or use Cm
dExec jobsteps to execute
sqlmaint.exe).
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"'" <anonymous@.discussions.microsoft.com> wrote in message news:1c53c01c452bd$3c2bda60$a301
280a@.phx.gbl...
> I got a maintenance plan that does a checkdb everyday at
> 1:00 pm (for example). If i add a job to this maintenance
> plan (going to job, adding a step to the job that
> exectutes the maintenance plan..) it works fien but when
> i open my maintenance plan to alter it (ex: add a
> database) i get an error and all my setting are erased.
> If i reconfigure it, it creates ANOTHER job using the
> previous name and adding a 1 to it.. anyone know why this
> is happeneing and how can i work arround this?!?!?!?

Question

In Oracle, you can write a statement that uses the "&" as a variable,

For example, select * from tbl_Stats where operator = '&Enter_Operator_Name'. The end user will enter an operator_name.

How do you do this in transact SQL for SQL Server?

Thanks

Lystrau have to pass as parameter in sql server


--eg:

create procedure sample_sp
(
@.Enter_Operator_Name as varchar(100)
)
as

select * from tbl_Stats where operator = @.Enter_Operator_Name|||For example, select * from tbl_Stats where operator = '&Enter_Operator_Name'. The end user will enter an operator_name.
Dunno much about Oracle - but this sounds rather like Access which also is an integrated UI & dbms.

The gist with SQL Server is that the app will have to get the parameter value itself and then pass this to the sproc (a la mallier's answer).|||The procedure was created successfully, however, it did not prompt me to enter an operator name. It is telling me I need to declare my variable @.Enter_Operator_Name.

Any suggestions?|||The procedure was created successfully, however, it did not prompt me to enter an operator name. It is telling me I need to declare my variable @.Enter_Operator_Name.

Any suggestions?

T-SQL is pretty different from Oracle with respect to handling variables. In T-SQL, you are going to end up having to do something like:

DECLARE @.MyVar int

SELECT @.MyVar = 100

SELECT * FROM tblCustomers WHERE CustID = @.MyVar

At runtime, you will have to replace the '= 100' with a value that you supply. Using SQLPlus, I know that you can do things from the command line calling files and replacing variables on the fly with parameters that you supply from the command line. This is doable in SQL Server as well, but you will need to use osql instead of the Query Analyzer to achieve the same effect (and even then I don't know exactly how to do it since I am not an osql guru).

Regards,

hmscott

Edit to add: You said "Procedure". Is this a stored proc you are trying to create? If so, in your stored proce declaration you can do this:

CREATE PROC spGetCustomer (@.CustID int)

AS

SET NOCOUNT ON

SELECT * FROM tblCustomer WHERE CustID = @.CustID|||SQL*Plus is an environment tool as well as a script execution tool. You can compare it to Microsoft Access in that respect, in that both allow easy integration between the UI and the database engine.

A pure database scripting tool can't (and shouldn't) support user interaction. It needs to execute scripts, and only execute scripts. If the data isn't coded into the script, you really need a different tool (such as VBS or Microsoft Access) to properly implement that functionality.

-PatP